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

    代码
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public abstract class Graphics
    {
        
    public abstract void Draw();
    }

    public class Picture:Graphics
    {
        
    protected ArrayList list=new ArrayList();
        
    public override void Draw()
        {
            
    foreach(Graphics g in list)
            {
                g.Draw();
            }
        }
        
        
    public void Add(Graphics g)
        {
            list.Add(g);
        }
        
        
    public void Remove(Graphics g)
        {
            list.Remove(g);
        }
    }
    public class Line:Graphics
    {
        
    public override void Draw()
        {
            Console.WriteLine(
    "Line Draw();");
        }
    }

    public class Circle:Graphics
    {
        
    public override void Draw()
        {
            Console.WriteLine(
    "Circle Draw();");
        }
    }

    public class Rectangle:Graphics
    {
        
    public override void Draw()
        {
            Console.WriteLine(
    "Rectangle Draw();");
        }
    }

    public class MyClass
    {
        
    public static void Main()
        {
            Picture myPicture
    =new Picture();
            myPicture.Add(
    new Line());
            myPicture.Add(
    new Circle());
            
            Rectangle myRec
    =new Rectangle();
            myPicture.Add(myRec);
            
            myPicture.Draw();
            Console.ReadLine();
        }
    }

    using System;
    using System.Collections;
    using System.Collections.Generic;

    public abstract class Component
    {
        protected string name;
        public Component(string name)
        {
            this.name=name;
        }
        public abstract void Display(int depth);
    }
    public class Composite:Component
    {
        private ArrayList children=new ArrayList();
        
        public Composite(string name):base(name)
        {}
        
        public void Add(Component component)
        {
            children.Add(component);
        }
        public void Remove(Component component)
        {
            children.Remove(component);
        }
        
        public override void Display(int depth)
        {
            Console.WriteLine(new String('-',depth)+this.name);
        }
    }

    public class Leaf:Component
    {
        public Leaf(string name):base(name)
        {
            
        }
        public override void Display(int depth)
        {
            Console.WriteLine(new String('-',depth)+this.name);
        }
    }

    public class MyClass
    {
        public static void Main()
        {
            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);
            root.Add(new Leaf("Leaf C"));
            root.Add(new Leaf("Leaf D"));
            
            Leaf l=new Leaf("Leaf E");
            root.Add(l);
            root.Remove(l);
            
            root.Display(2);
            
            Console.ReadLine();
            
            
        }
    }

  • 相关阅读:
    【转载】用XML和XSLT来生成静态的HTML页面
    【转载】Lambda表达式(Lambda Expressions)
    [转]打领带的十种方法
    读书笔记
    【转载】用手机的朋友进来看看吧,终身受益啊!!!
    SQL查询出重复出现的数据
    技巧三:字符串格式化
    【Vegas原创】页面自动跳转代码收集
    【Vegas原创】我写的一个安装windowsService的BAT
    【Vegas原创】ASP.NET读取Excel,并以邮件正文方式和附件方式发送实例
  • 原文地址:https://www.cnblogs.com/mikechang/p/1708870.html
Copyright © 2011-2022 走看看