组合模式

组合模式

  1:解决对象“整体-部分”的问题。

  2:用户处理对象的组合和单个对象的时候,希望能够有一个统一的方式来处理。(即继承同一个类或者接口)

因为对象的组合一般含有单个对象之外的方法,所以父类或者接口的抽象方法一般有两种:

  1.与组合对象的方法相同:即透明模式。(调用单个对象的时候,有些方法未实现,需要throw)

  2.与单个对象的方法相同:即安全模式。

1.透明模式:

    class Program
    {
        static void Main(string[] args)
        {
            ComplexGraphics complexGraphics = new ComplexGraphics("one ComplexGraphics and two Lines");
            complexGraphics.Add(new Line("Line A"));
            ComplexGraphics CompositeCG = new ComplexGraphics("Cricle and Line");
            CompositeCG.Add(new Circle("Cricle A"));
            CompositeCG.Add(new Circle("Line B"));
            complexGraphics.Add(CompositeCG);
            Line l = new Line("Line C");
            complexGraphics.Add(l);

            // 显示复杂图形的画法
            Console.WriteLine("Draw ComplexGraphics:");
            Console.WriteLine("---------------------");
            complexGraphics.Draw();
            Console.WriteLine("Finish Draw");
            Console.WriteLine("---------------------");
            Console.WriteLine();

            // 移除一个组件再显示复杂图形的画法
            complexGraphics.Remove(l);
            Console.WriteLine("Remove Line C,Draw ComplexGraphics:");
            Console.WriteLine("---------------------");
            complexGraphics.Draw();
            Console.WriteLine("Finish Draw");
            Console.WriteLine("---------------------");
            Console.Read();
        }
    }

    /// <summary>
    /// 图形
    /// </summary>
    public abstract class Graphics
    {
        public string Name { get; set; }

        public Graphics(string name)
        {
            this.Name = name;
        }

        public abstract void Draw();

        public abstract void Add(Graphics g);

        public abstract void Remove(Graphics g);
    }

    /// <summary>
    /// 简单线
    /// </summary>
    public class Line : Graphics
    {
        public Line(string name)
            : base(name)
        { }

        public override void Add(Graphics g)
        {
            throw new Exception("不能向简单图形Line中添加其他图形");
        }

        public override void Draw()
        {
            Console.WriteLine("Line " + Name);
        }

        public override void Remove(Graphics g)
        {
            throw new Exception("不能向简单图形Line中移除其他图形");
        }
    }

    /// <summary>
    /// 简单圆
    /// </summary>
    public class Circle : Graphics
    {
        public Circle(string name)
            : base(name)
        { }

        public override void Add(Graphics g)
        {
            throw new Exception("不能向简单图形Circle中添加其他图形");
        }

        public override void Draw()
        {
            Console.WriteLine("Line " + Name);
        }

        public override void Remove(Graphics g)
        {
            throw new Exception("不能向简单图形Circle中移除其他图形");
        }
    }

    /// <summary>
    /// 复杂图形
    /// </summary>
    public class ComplexGraphics : Graphics
    {
        public List<Graphics> complexGraphicsList = new List<Graphics>();
        public ComplexGraphics(string name)
            : base(name)
        { }

        public override void Draw()
        {
            foreach (var g in complexGraphicsList) {
                g.Draw();
            }
        }

        public override void Add(Graphics g)
        {
            complexGraphicsList.Add(g);
        }

        public override void Remove(Graphics g)
        {
            complexGraphicsList.Remove(g);
        }
    }

2.安全模式:

    class Program
    {
        static void Main(string[] args)
        {
            ComplexGraphics complexGraphics = new ComplexGraphics("one ComplexGraphics and two Lines");
            complexGraphics.Add(new Line("Line A"));
            ComplexGraphics CompositeCG = new ComplexGraphics("Cricle and Line");
            CompositeCG.Add(new Circle("Cricle A"));
            CompositeCG.Add(new Circle("Line B"));
            complexGraphics.Add(CompositeCG);
            Line l = new Line("Line C");
            complexGraphics.Add(l);

            // 显示复杂图形的画法
            Console.WriteLine("Draw ComplexGraphics:");
            Console.WriteLine("---------------------");
            complexGraphics.Draw();
            Console.WriteLine("Finish Draw");
            Console.WriteLine("---------------------");
            Console.WriteLine();

            // 移除一个组件再显示复杂图形的画法
            complexGraphics.Remove(l);
            Console.WriteLine("Remove Line C,Draw ComplexGraphics:");
            Console.WriteLine("---------------------");
            complexGraphics.Draw();
            Console.WriteLine("Finish Draw");
            Console.WriteLine("---------------------");
            Console.Read();
        }
    }

    /// <summary>
    /// 图形
    /// </summary>
    public abstract class Graphics
    {
        public string Name { get; set; }

        public Graphics(string name)
        {
            this.Name = name;
        }

        public abstract void Draw();
    }

    /// <summary>
    /// 简单线
    /// </summary>
    public class Line : Graphics
    {
        public Line(string name)
            : base(name)
        { }

        public override void Draw()
        {
            Console.WriteLine("Line " + Name);
        }
    }

    /// <summary>
    /// 简单圆
    /// </summary>
    public class Circle : Graphics
    {
        public Circle(string name)
            : base(name)
        { }

        public override void Draw()
        {
            Console.WriteLine("Line " + Name);
        }
    }

    /// <summary>
    /// 复杂图形
    /// </summary>
    public class ComplexGraphics : Graphics
    {
        public List<Graphics> complexGraphicsList = new List<Graphics>();
        public ComplexGraphics(string name)
            : base(name)
        { }

        public override void Draw()
        {
            foreach (var g in complexGraphicsList) {
                g.Draw();
            }
        }

        public void Add(Graphics g)
        {
            complexGraphicsList.Add(g);
        }

        public void Remove(Graphics g)
        {
            complexGraphicsList.Remove(g);
        }
    }

 

posted @ 2015-05-29 11:48  江境纣州  阅读(92)  评论(0编辑  收藏  举报