⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compositerealworld.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Collections;

//组合模式(Composite)
//意图
//    将对象组合成树形结构以表示“部分-整体”的层次结构。Composite 使得用户对单个对象和组合对象的使用具有一致性。
//适用性
//    1.你想表示对象的部分-整体层次结构。
//    2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
//例子
//    CppUnit 中的 TestCase(测试用例) 和 TestSuite(测试包) -- 两者都从 Test 继承,都实现纯虚函数 run
//问题
//    对于 Leaf 是否需要Add方法的讨论 ???

namespace DesignPattern.CompositeRealWorld
{
    class CompositeRealWorld : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            // 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) 
        {
            DesignPattern.FormMain.OutputInfo( "Cannot add to a PrimitiveElement"); 
        }
        public override void Remove(DrawingElement c) 
        {
            DesignPattern.FormMain.OutputInfo( "Cannot remove from a PrimitiveElement"); 
        }
        public override void Display(int indent) 
        {
            DesignPattern.FormMain.OutputInfo(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) 
        {
            DesignPattern.FormMain.OutputInfo(new String('-', indent) + "+ " + name);
            // Display each child element on this node 
            foreach (DrawingElement c in elements) 
            {
                c.Display(indent + 2); 
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -