设计模式——复合模式

名称 Composite
结构  
意图 将对象组合成树形结构以表示“部分-整体”的层次结构。C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性。
适用性
  • 你想表示对象的部分-整体层次结构。
  • 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
Code Example
  1// Composite
  2
  3// Intent: "Compose objects into tree structures to represent part-whole 
  4// hierarchies. Composite lets clients treat individual objects and 
  5// compositions of objects uniformly". 
  6
  7// For further information, read "Design Patterns", p163, Gamma et al.,
  8// Addison-Wesley, ISBN:0-201-63361-2
  9
 10/* Notes:
 11 * We often have tree structures where some nodes are containers of 
 12 * other nodes, and some nodes are leafs. Rather than have separate client
 13 * code to manage each type of node, Composite lets a client work with
 14 * either using the same code. 
 15 * 
 16 * There is an excellent discussion to be found in "Pattern Hatching", 
 17 * Vlissides, ISBN: 0-201-43293-5, describing how the Composite design
 18 * pattern can be used as the basis file system management 
 19 * (directory = composite, Leaf = file, Component = iNode)
 20 * 
 21 */

 22 
 23namespace FactoryMethod_DesignPattern
 24{
 25    using System;
 26    using System.Collections;
 27
 28    abstract class Component 
 29    {
 30        protected string strName;
 31
 32        public Component(string name)
 33        {
 34            strName = name;
 35        }

 36
 37        abstract public void Add(Component c);
 38    
 39        public abstract void DumpContents();
 40        
 41        // other operations for delete, get, etc.
 42    }

 43
 44    class Composite : Component
 45    {
 46        private ArrayList ComponentList = new ArrayList();
 47        
 48        public Composite(string s) : base(s) {}
 49
 50        override public void Add(Component c)
 51        {
 52            ComponentList.Add(c);
 53        }

 54
 55        public override void DumpContents()
 56        {
 57            // First dump the name of this composite node
 58            Console.WriteLine("Node: {0}", strName);
 59
 60            // Then loop through children, and get then to dump their contents
 61            foreach (Component c in ComponentList)
 62            {
 63                c.DumpContents();
 64            }

 65        }

 66    }

 67
 68    class Leaf : Component
 69    {
 70        public Leaf(string s) : base(s) {}
 71
 72        override public void Add(Component c)
 73        {
 74            Console.WriteLine("Cannot add to a leaf");
 75        }

 76
 77        public override void DumpContents()
 78        {
 79            Console.WriteLine("Node: {0}", strName);
 80        }

 81    }

 82
 83    /// <summary>
 84    ///    Summary description for Client.
 85    /// </summary>

 86    public class Client
 87    {
 88        Component SetupTree()
 89        {
 90            // here we have to create a tree structure, 
 91            // consisting of composites and leafs.     
 92            Composite root = new Composite("root-composite");
 93            Composite parentcomposite;
 94            Composite composite;
 95            Leaf leaf;
 96
 97            parentcomposite = root;
 98            composite = new Composite("first level - first sibling - composite");
 99            parentcomposite.Add(composite);
100            leaf = new Leaf("first level - second sibling - leaf");
101            parentcomposite.Add(leaf);
102            parentcomposite = composite; 
103            composite = new Composite("second level - first sibling - composite");
104            parentcomposite.Add(composite);
105            composite = new Composite("second level - second sibling - composite");
106            parentcomposite.Add(composite);
107
108            // we will leaf the second level - first sibling empty, and start 
109            // populating the second level - second sibling 
110            parentcomposite = composite; 
111            leaf = new Leaf("third level - first sibling - leaf");
112            parentcomposite.Add(leaf);
113            
114            leaf = new Leaf("third level - second sibling - leaf");
115            parentcomposite.Add(leaf);
116            composite = new Composite("third level - third sibling - composite");
117            parentcomposite.Add(composite);
118
119            return root;
120        }

121
122        public static int Main(string[] args)
123        {   
124               Component component;
125            Client c = new Client();
126            component = c.SetupTree();
127
128            component.DumpContents();
129            return 0;
130        }

131    }

132}

133
134
posted @ 2005-06-29 08:43  DarkAngel  阅读(1082)  评论(0编辑  收藏  举报