点点滴滴


         从点开始绘制自己的程序人生
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Composite

Posted on 2006-10-29 15:10  点点滴滴  阅读(186)  评论(0编辑  收藏  举报
// Composite pattern -- Real World example

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Composite.RealWorld
{
  
  // Mainapp test application

  class MainApp
  {
    static void Main()
    {
      // Create a tree structure
      CompositeElement root =
        new CompositeElement("Picture");
      root.Add(new PrimitiveElement("Red Line"));
      root.Add(new PrimitiveElement("Blue Circle"));
      root.Add(new PrimitiveElement("Green Box"));

      CompositeElement comp =
        new CompositeElement("Two Circles");
      comp.Add(new PrimitiveElement("Black Circle"));
      comp.Add(new PrimitiveElement("White Circle"));
      root.Add(comp);

      // Add and remove a PrimitiveElement
      PrimitiveElement pe =
        new PrimitiveElement("Yellow Line");
      root.Add(pe);
      root.Remove(pe);

      // Recursively display nodes
      root.Display(1);

      // Wait for user
      Console.Read();
    }
  }

  // "Component" Treenode

  abstract class DrawingElement
  {
    protected string name;

    // Constructor
    public DrawingElement(string name)
    {
      this.name = name;
    }

    public abstract void Add(DrawingElement d);
    public abstract void Remove(DrawingElement d);
    public abstract void Display(int indent);
  }

  // "Leaf"

  class PrimitiveElement : DrawingElement
  {
    // Constructor
    public PrimitiveElement(string name) : base(name)
    {  
    }

    public override void Add(DrawingElement c)
    {
      Console.WriteLine(
        "Cannot add to a PrimitiveElement");
    }

    public override void Remove(DrawingElement c)
    {
      Console.WriteLine(
        "Cannot remove from a PrimitiveElement");
    }

    public override void Display(int indent)
    {
      Console.WriteLine(
        new String('-', indent) + " " + name);
    }
  }

  // "Composite"

  class CompositeElement : DrawingElement
  {
    private ArrayList elements = new ArrayList();
  
    // Constructor
    public CompositeElement(string name) : base(name)
    {  
    }

    public override void Add(DrawingElement d)
    {
      elements.Add(d);
    }

    public override void Remove(DrawingElement d)
    {
      elements.Remove(d);
    }

    public override void Display(int indent)
    {
      Console.WriteLine(new String('-', indent) +
        "+ " + name);

      // Display each child element on this node
      foreach (DrawingElement c in elements)
      {
        c.Display(indent + 2);
      }
    }
  }
}