zoukankan      html  css  js  c++  java
  • GoF——组合模式

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

    结构图:

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

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

  • 相关阅读:
    POJ 3140 Contestants Division (树dp)
    POJ 3107 Godfather (树重心)
    POJ 1655 Balancing Act (树的重心)
    HDU 3534 Tree (经典树形dp)
    HDU 1561 The more, The Better (树形dp)
    HDU 1011 Starship Troopers (树dp)
    Light oj 1085
    Light oj 1013
    Light oj 1134
    FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
  • 原文地址:https://www.cnblogs.com/virus1102/p/Composite.html
Copyright © 2011-2022 走看看