• 分公司 = 一部门 组合模式


    19.1 分公司不就是一部门吗?

    整体与部分可以被一致对待,

    19.2 组合模式

    将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性,

    namespace 组合模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Composite root = new Composite("root");                    //生成树根root,跟上长出两叶LeafA和LeafB,
                root.Add(new Leaf("Leaf A"));
                root.Add(new Leaf("Leaf B"));
    
                Composite comp = new Composite("Composite X");             //根上长出分枝CompositeX,分枝上也有两叶LeafXA和LeafXB,
                comp.Add(new Leaf("Leaf XA"));
                comp.Add(new Leaf("Leaf XB"));
                root.Add(comp);
    
                Composite comp2 = new Composite("Composite XY");           //在CompositeX上在长出分枝CompositeXY,分枝上也有两叶LeafXYA和LeafXYB,
                comp2.Add(new Leaf("Leaf XYA"));
                comp2.Add(new Leaf("Leaf XYB"));
                comp.Add(comp2);
    
                root.Add(new Leaf("Leaf C"));                              //根部又长出两叶LeafC和LeafD,可惜LeafD没长牢被风吹走了,
                Leaf leaf = new Leaf("Leaf D");
                root.Add(leaf);
                root.Remove(leaf);
    
                root.Display(1);
    
                Console.Read();
            }
        }
    
        //Component为组合中的对象接口声明,
        //适当情况下,实现所有类共有接口的默认行为,
        //声明一个接口用于访问和管理Component的子部件,
        abstract class Component
        {
            protected string name;
    
            public Component(string name)
            {
                this.name = name;
            }
    
            public abstract void Add(Component c);              //通常用Add和Remove方法来提供增加和移除树枝或树叶的过程,
    
            public abstract void Remove(Component c);
    
            public abstract void Display(int depth);
        }
    
        //Composite定义有枝节点行为,用来存储子部件,
        //在Component接口中实现与子部件有关的操作,如Add与Remove,
        class Composite : Component
        {
            //一个子对象集合用来存储其下属的枝节点和叶节点,
            private List<Component> children = new List<Component>();
    
            public Composite(string name)
                : base(name)
            { }
    
            public override void Add(Component c)
            {
                children.Add(c);
            }
    
            public override void Remove(Component c)
            {
                children.Remove(c);
            }
    
            //显示其枝节点名称,并对其下级进行遍历,
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
    
                foreach (Component component in children)
                {
                    component.Display(depth + 2);
                }
            }
        }
    
        //表示叶节点对象,叶节点没有子节点,
        class Leaf : Component
        {
            public Leaf(string name)
                : base(name)
            { }
    
            public override void Add(Component c)                             //由于叶子没有在再增加分枝和树叶,所以Add和Remove方法实现它没有意义,
            {                                                                 //但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口,
                Console.WriteLine("Cannot add to a leaf");
            }
    
            public override void Remove(Component c)
            {
                Console.WriteLine("Cannot remove from a leaf");
            }
    
            public override void Display(int depth)                           //叶节点的具体方法,
            {
                Console.WriteLine(new String('-', depth) + name);
            }
        }
    
    }
    View Code

    19.3 透明方式与安全方式

    透明模式,在Component中声明所有用来管理子类对象的方法,其中包括Add和Remove等,这样实现Component接口的所有子类都具备了Add和Remove,这样做的好处就是叶节点和枝节点对于外界没有区别,它们具备完全一致的行为接口,但问题也很明显,因为Leaf类本身不具备Add和Remove方法的功能,所以实现它是没有意义的,

    安全方式,树枝类和树叶类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不便,

    19.4 何时使用组合模式

    当需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,同一的使用组合结构中的所有对象时,就应该考虑用组合模式了,

    19.5 公司管理系统

    namespace 组合模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConcreteCompany root = new ConcreteCompany("北京总公司");
                root.Add(new HRDepartment("总公司人力资源部"));
                root.Add(new FinanceDepartment("总公司财务部"));
    
                ConcreteCompany comp = new ConcreteCompany("上海华东分公司");
                comp.Add(new HRDepartment("华东分公司人力资源部"));
                comp.Add(new FinanceDepartment("华东分公司财务部"));
                root.Add(comp);
    
                ConcreteCompany comp1 = new ConcreteCompany("南京办事处");
                comp1.Add(new HRDepartment("南京办事处人力资源部"));
                comp1.Add(new FinanceDepartment("南京办事处财务部"));
                comp.Add(comp1);
    
                ConcreteCompany comp2 = new ConcreteCompany("杭州办事处");
                comp2.Add(new HRDepartment("杭州办事处人力资源部"));
                comp2.Add(new FinanceDepartment("杭州办事处财务部"));
                comp.Add(comp2);
    
                Console.WriteLine("
    结构图:");
    
                root.Display(1);
    
                Console.WriteLine("
    职责:");
    
                root.LineOfDuty();
    
                Console.Read();
            }
        }
    
        //公司类,抽象或接口,
        abstract class Company
        {
            protected string name;
    
            public Company(string name)
            {
                this.name = name;
            }
    
            public abstract void Add(Company c);                      //增加,
            public abstract void Remove(Company c);                   //移除,
            public abstract void Display(int depth);                  //显示,
            public abstract void LineOfDuty();                        //履行职责,
        }
    
        //具体公司类,实现接口,树枝节点,
        class ConcreteCompany : Company
        {
            private List<Company> children = new List<Company>();
    
            public ConcreteCompany(string name)
                : base(name)
            { }
    
            public override void Add(Company c)
            {
                children.Add(c);
            }
    
            public override void Remove(Company c)
            {
                children.Remove(c);
            }
    
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
    
                foreach (Company component in children)
                {
                    component.Display(depth + 2);
                }
            }
    
            //履行职责,
            public override void LineOfDuty()
            {
                foreach (Company component in children)
                {
                    component.LineOfDuty();
                }
            }
        }
    
        //人力资源部,
        class HRDepartment : Company
        {
            public HRDepartment(string name)
                : base(name)
            { }
    
            public override void Add(Company c)
            {
    
            }
    
            public override void Remove(Company c)
            {
    
            }
    
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
            }
    
            public override void LineOfDuty()
            {
                Console.WriteLine("{0} 员工招聘培训管理", name);
            }
        }
    
        //财务部,
        class FinanceDepartment : Company
        {
            public FinanceDepartment(string name)
                : base(name)
            { }
    
            public override void Add(Company c)
            {
    
            }
    
            public override void Remove(Company c)
            {
    
            }
    
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
            }
    
            public override void LineOfDuty()
            {
                Console.WriteLine("{0} 公司财务收支管理", name);
            }
        }
    
    }
    View Code

    19.6 组合模式的好处

    组合模式这样就定义了包含人力资源部和财务部这些基本对象和分公司,办事处等组合对象的类层次结构,基本对象可以被组合成更复杂的对象,而这个组合对象又可以被组合,这样的不断递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象了,

    用户是不用关心到底是处理一个叶节点还是处理一个组合组件,也就用不着为定义组合而写一些选择判断语句了,

    简单的说,就是组合模式让客户可以一致的使用组合结构和单个对象,

  • 相关阅读:
    bzoj 1053: [HAOI2007]反素数ant 搜索
    bzoj 1040: [ZJOI2008]骑士 環套樹DP
    对主席树的一些理解
    POJ 2777 Count Color【线段树】
    POJ 2420 A Star not a Tree?【爬山法】
    生物遗传学 整理人PYJ (恋_紫花地丁)
    POJ3321Apple Tree【dfs 树状数组】
    POJ1450:Gridland 【杂题】
    POJ 1088 滑雪【记忆化搜索】
    Poj3253:Fence Repair 【贪心 堆】
  • 原文地址:https://www.cnblogs.com/huangxuQaQ/p/11301699.html
Copyright © 2020-2023  润新知