zoukankan      html  css  js  c++  java
  • 组合模式之拼凑的美好

            廊坊的风一如既往的在窗外刮着,天地间肆意地飘洒,纵情在一刹那,为何如今仅仅剩下风吹乱我的发,乱蓬蓬的,还是去超市逛逛吧,买吃的`(*∩_∩*)′,走进华联超市,热情的店员招呼着我,開始为我介绍,推荐各种各样商品,店员向我推荐了他们的会员卡,全场全部项目均八折,每逢节假日打五折,我心想那太划算了,并且他们总店,分店,加盟店都能够用,所以就办了张会员卡。今天我们的设计模式就从超市会员卡開始说起。

            这个俨然就是我们设计模式中的组合模式----组合模式有时候又叫做部分-总体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序能够向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。看以下的两幅图片:

                

            上面两幅图片我们能够看做是一个文件结构,对于这种结构我们称之为树形结构。在《大话设计模式》中我们了解到能够通过调用某个方法来遍历整个树,当我们找到某个叶子节点后,就能够对叶子节点进行相关的操作。我们能够将这颗树理解成一个大的容器,容器里面包括非常多的成员对象,这些成员对象就可以是容器对象也能够是叶子对象。可是因为容器对象和叶子对象在功能上面的差别,使得我们在使用的过程中必需要区分容器对象和叶子对象,可是这样就会给客户带来不必要的麻烦,对于客户而言,始终希望能够一致的对待容器对象和叶子对象。这就是组合模式的设计动机:组合模式定义了怎样将容器对象和叶子对象进行递归组合,使得客户在使用的过程中无须进行区分,能够对他们进行一致的处理。以下一起来看一下组合模式的结构图:

           

          以上述在华联超市办会员卡为例,看看组合模式是怎样在代码中实现:

           

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                StoreOrBranch store = new StoreOrBranch("北京总店");
                StoreOrBranch brach = new StoreOrBranch("廊坊分店");
                JoinInStore jstore = new JoinInStore("安徽加盟店一");
                JoinInStore jstore1 = new JoinInStore("海南加盟店二");
    
                brach.Add(jstore);
                brach.Add(jstore1);
                store.Add(brach);
    
                store.PayByCard();
            }
        }
    
    
        /// <summary>
        /// 店面类 抽象出来的店面部件
        /// </summary>
        public abstract class Storefront
        {
            //店名
            protected string storeName = string.Empty;
            public string StoreName
            {
                get
                {
                    return storeName;
                }
            }
    
            //加入店面
            public abstract void Add(Storefront store);
            //删除店面
            public abstract void Remove(Storefront store);
    
            //定义所有部件公用的行为 刷卡行为
            public abstract void PayByCard();
        }
    
        public class StoreOrBranch : Storefront
        {
            //构造函数
            public StoreOrBranch() { }
            public StoreOrBranch(string storeName)
            {
                this.storeName = storeName;
            }
            List<Storefront> myStoreList = new List<Storefront>();
            //刷卡消费
            public override void PayByCard()
            {
                Console.WriteLine("店面{0}的积分已累加进该会员卡", storeName);
                foreach (Storefront sf in myStoreList)
                {
                    sf.PayByCard();
                }
            }
    
            //添加店面
            public override void Add(Storefront store)
            {
                myStoreList.Add(store);
            }
    
            //解除店面
            public override void Remove(Storefront store)
            {
                myStoreList.Remove(store);
            }
        }
    
        public class JoinInStore : Storefront
        {
            //构造函数
            public JoinInStore() { }
            public JoinInStore(string storeName)
            {
                this.storeName = storeName;
            }
            //刷卡消费
            public override void PayByCard()
            {
                Console.WriteLine("店面{0}的积分已累加进该会员卡", storeName);
            }
    
            public override void Add(Storefront store)
            { }
    
            public override void Remove(Storefront store)
            { }
        }
    
       
    }
    
             组合模式的目的是:让client不再区分操作的是组合对象还是叶子对象,而是以一个统一的方式来操作。实现这个目标的关键之处,是设计一个抽象的组件类,让它能够代表组合对象和叶子对象。这样一来,client就不用区分究竟是组合对象还是叶子对象了,仅仅须要所有当成组件对象进行统一的操作就能够了。

           


  • 相关阅读:
    druid:阿里巴巴开源,数据库连接池管理
    各JAVA开发框架版本及对应信息
    各版本区别
    MyBatis 知识点
    java的关键字:static、final
    请求转发(Forward)和重定向(Redirect)的区别
    Spring 向页面传值以及接受页面传过来的参数的方式
    Spring 框架中 ModelAndView、Model、ModelMap 的区别
    Connection: keep-alive,Content-Length,Transfer-Encoding: chunked,Content-Encoding: gzip等
    git 报错及解决
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3758550.html
Copyright © 2011-2022 走看看