组合模式

http://blog.csdn.net/cxllyg/article/details/8104458

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

范例一:

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace 组合模式  
  6. {  
  7.     abstract class Component  
  8.     {  
  9.         protected string name;  
  10.   
  11.         public Component(string name)  
  12.         {  
  13.             this.name = name;  
  14.         }  
  15.   
  16.         public abstract void Add(Component c);  
  17.         public abstract void Remove(Component c);  
  18.         public abstract void Display(int depth);  
  19.     }  
  20.   
  21.     class Leaf : Component  
  22.     {  
  23.         public Leaf(string name) : base(name) { }  
  24.         public override void Add(Component c)  
  25.         {  
  26.             Console.WriteLine("Cannot add to a leaf");  
  27.         }  
  28.   
  29.         public override void Remove(Component c)  
  30.         {  
  31.             Console.WriteLine("Cannot remove from a leaf");  
  32.         }  
  33.   
  34.         public override void Display(int depth)  
  35.         {  
  36.             Console.WriteLine(new String('-', depth) + name);  
  37.         }  
  38.     }  
  39.   
  40.     class Composite : Component  
  41.     {  
  42.         private List<Component> children = new List<Component>();  
  43.         public Composite(string name) : base(name) { }  
  44.   
  45.         public override void Add(Component c)  
  46.         {  
  47.             children.Add(c);  
  48.         }  
  49.   
  50.         public override void Remove(Component c)  
  51.         {  
  52.             children.Remove(c);  
  53.         }  
  54.   
  55.         public override void Display(int depth)  
  56.         {  
  57.             Console.WriteLine(new String('-', depth) + name);  
  58.   
  59.             foreach (Component component in children)  
  60.             {  
  61.                 component.Display(depth + 2);  
  62.             }  
  63.         }  
  64.           
  65.   
  66.     }  
  67.   
  68.     class Program  
  69.     {  
  70.         static void Main(string[] args)  
  71.         {  
  72.             Composite root = new Composite("root");  
  73.             root.Add(new Leaf("Leaf A"));  
  74.             root.Add(new Leaf("Leaf B"));  
  75.   
  76.             Composite comp = new Composite("Composite X");  
  77.             comp.Add(new Leaf("Leaf XA"));  
  78.             comp.Add(new Leaf("Leaf XB"));  
  79.   
  80.             root.Add(comp);  
  81.   
  82.             Composite comp2 = new Composite("Composite XY");  
  83.             comp2.Add(new Leaf("Left XYA"));  
  84.             comp2.Add(new Leaf("Left XYB"));  
  85.             comp.Add(comp2);  
  86.   
  87.             root.Add(new Leaf("Leaf C"));  
  88.             Leaf leaf = new Leaf("Leaf D");  
  89.             root.Add(leaf);  
  90.             root.Remove(leaf);  
  91.   
  92.             root.Display(1);  
  93.   
  94.             Console.Read();  
  95.         }  
  96.     }  
  97. }  

 

范例二:公司管理系统

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace 组合模式_公司管理系统  
  6. {  
  7.     abstract class Company  
  8.     {  
  9.         protected string name;  
  10.   
  11.         public Company(string name)  
  12.         {  
  13.             this.name = name;  
  14.         }  
  15.   
  16.         public abstract void Add(Company c);  
  17.         public abstract void Remove(Company c);  
  18.         public abstract void Display(int depth);  
  19.     }  
  20.   
  21.     //具体公司类  
  22.     class ConcreteCompany : Company  
  23.     {  
  24.         private List<Company> children = new List<Company>();  
  25.   
  26.         public ConcreteCompany(string name) : base(name) { }  
  27.   
  28.         public override void Add(Company c)  
  29.         {  
  30.             children.Add(c);  
  31.         }  
  32.   
  33.         public override void Remove(Company c)  
  34.         {  
  35.             children.Remove(c);  
  36.         }  
  37.   
  38.         public override void Display(int depth)  
  39.         {  
  40.             Console.WriteLine(new string('-', depth) + name);  
  41.             foreach (Company component in children)  
  42.             {  
  43.                 component.Display(depth + 2);  
  44.             }  
  45.         }  
  46.     }  
  47.   
  48.     //人力资源部  
  49.     class HRDepartment : Company  
  50.     {  
  51.         public HRDepartment(string name) : base(name) { }  
  52.   
  53.         public override void Add(Company c)  
  54.         {  
  55.               
  56.         }  
  57.   
  58.         public override void Remove(Company c)  
  59.         {  
  60.               
  61.         }  
  62.   
  63.         public override void Display(int depth)  
  64.         {  
  65.             Console.WriteLine(new string('-', depth) + name);  
  66.         }  
  67.     }  
  68.   
  69.     //财务部  
  70.     class FinanceDepartment : Company  
  71.     {  
  72.         public FinanceDepartment(string name) : base(name) { }  
  73.   
  74.         public override void Add(Company c)  
  75.         {  
  76.   
  77.         }  
  78.   
  79.         public override void Remove(Company c)  
  80.         {  
  81.   
  82.         }  
  83.   
  84.         public override void Display(int depth)  
  85.         {  
  86.             Console.WriteLine(new string('-', depth) + name);  
  87.         }  
  88.     }  
  89.   
  90.     class Program  
  91.     {  
  92.         static void Main(string[] args)  
  93.         {  
  94.             ConcreteCompany root=new ConcreteCompany("北京总公司");  
  95.             root.Add(new HRDepartment("总公司人力资源部"));  
  96.             root.Add(new FinanceDepartment("总公司财务部"));  
  97.   
  98.             ConcreteCompany comp = new ConcreteCompany("上海华东分公司");  
  99.             comp.Add(new HRDepartment("华东分公司人力资源部"));  
  100.             comp.Add(new FinanceDepartment("华东分公司财务部"));  
  101.             root.Add(comp);  
  102.   
  103.             ConcreteCompany comp1 = new ConcreteCompany("南京办事处");  
  104.             comp1.Add(new HRDepartment("南京办事处人力资源部"));  
  105.             comp1.Add(new FinanceDepartment("南京办事处财务部"));  
  106.             root.Add(comp1);  
  107.   
  108.             ConcreteCompany comp2 = new ConcreteCompany("杭州办事处");  
  109.             comp2.Add(new HRDepartment("杭州办事处人力资源部"));  
  110.             comp2.Add(new FinanceDepartment("杭州办事处财务部"));  
  111.             root.Add(comp2);  
  112.   
  113.             root.Display(1);  
  114.   
  115.             Console.Read();  
  116.         }  
  117.     }  
  118. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值