设计模式之组合模式

来源:互联网 发布:手机淘宝怎么找货源 编辑:程序博客网 时间:2024/06/10 02:11

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

Created with Raphaël 2.1.0LeafLeafComponentComponentCompositeComposite继承继承对方并且与父类呈聚合关系

抽象类:
Component: 方法: Add(Component c) Remove(Component c) Display(int depth)

子类:
Leaf: Display(int depth)
子类(具体类)
Composite: Add(Component c) Remove(Component c) Display(int depth)

public abstract class Component    {        protected string name;        public Component(string name)        {            this.name = name;        }        public abstract void Add(Component c);        public abstract void Remove(Component c);        public abstract void Display(int depth);    }    //叶节点    public class Leaf : Component     {        public Leaf(string name) : base(name)         {        }        public override void Add(Component c)        {            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);        }    }    //枝节点    public 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 item in children)            {                item.Display(depth+2);            }        }    }

Main方法调用:

  static void Main(string[] args)        {            Composite root = new Composite("root");            root.Add(new Leaf("Leaf A"));            root.Add(new Leaf("Leaf B"));            Composite comp = new Composite("Composite X");            comp.Add(new Leaf("Leaf XA"));            comp.Add(new Leaf("Leaf XB"));            root.Add(comp);            Composite comp2 = new Composite("Composite XY");            comp2.Add(new Leaf("Leaf XYA"));            comp2.Add(new Leaf("Leaf XYB"));            root.Add(comp2);            root.Add(new Leaf("Leaf C"));            Leaf leaf = new Leaf("Leaf D");            root.Add(leaf);            root.Remove(leaf);            root.Display(1);            Console.ReadLine();        }

运行结果:
这里写图片描述

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

接下来,在练习一道题:
一个公司会有多个分公司,同时分公司和总公司都会有财务和人事部门。 它们具有相同的情况。

Created with Raphaël 2.1.0财务部财务部公司公司人事部人事部具体公司具体公司继承继承继承和聚合关系

个人建议,UML图再做题。实在是想不到的话可以看看下面的代码。或者自己做完后比较一番。这样可以加深印象。
提示: 抽象类公司 中包含的方法: 增加(公司 ) 减少(公司)
显示(int) 履行职责()

  //抽象工厂类    public 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 Show(int dept);        public abstract void LineOfDuty();    }    //具体公司    public class ConcreteCompany:Company     {        private List<Company> list = new List<Company>();        public ConcreteCompany(string name) : base(name)         {        }        public override void Add(Company c)        {            list.Add(c);        }        public override void Remove(Company c)        {            list.Remove(c);        }        public override void Show(int dept)        {            Console.WriteLine(new String('-',dept)+name);            foreach (var item in list)            {                item.Show(dept+2);            }        }        public override void LineOfDuty()        {            foreach (var item in list)            {                item.LineOfDuty();            }        }    }    //财务部    public class FinanceDepartment : Company     {        public FinanceDepartment(string name)            : base(name)        { }        public override void Add(Company c)        {        }        public override void Remove(Company c)        {        }        public override void Show(int dept)        {            Console.WriteLine(new String('-', dept)+name);        }        public override void LineOfDuty()        {            Console.WriteLine("我是{0}的,我负责发工资",name);        }    }    //人事部门    public class HRDepartment : Company    {        public HRDepartment(string name)            : base(name)        { }        public override void Add(Company c)        {        }        public override void Remove(Company c)        {        }        public override void Show(int dept)        {            Console.WriteLine(new String('-', dept)+name);        }        public override void LineOfDuty()        {            Console.WriteLine("我是{0}的,我负责招聘新员工",name);        }    }
 static void Main(string[] args)        {            ConcreteCompany c = new ConcreteCompany("总公司");            c.Add(new HRDepartment("北京总公司人事部"));            c.Add(new FinanceDepartment("北京总公司财务部"));            ConcreteCompany c1 = new ConcreteCompany("上海分公司");            c1.Add(new HRDepartment("上海分公司人事部"));            c1.Add(new FinanceDepartment("上海分公司财务部"));            c.Add(c1);            ConcreteCompany c2 = new ConcreteCompany("杭州分公司");            c2.Add(new HRDepartment("杭州分公司人事部"));            c2.Add(new FinanceDepartment("杭州分公司财务部"));            c.Add(c2);            c.Show(1);            c.LineOfDuty();            Console.ReadLine();        }

结果:
这里写图片描述

0 0
原创粉丝点击