zoukankan      html  css  js  c++  java
  • 组合(Composite)模式 *

    组合(Composite)模式:将对象组合树形结构以表示‘部分-整体’的层次结构。

     组合模式使得用户对单个对象和组合对象具有一致性 

    /*
    * 抽象构件(Component)角色:这是一个抽象角色,它给参与组合的对象规定一个接口。这个角色给出共有接口及其默认行为。
    * 树叶构件(Leaf)角色:代表参加组合的树叶对象。一个树叶对象没有下级子对象。
    * 树枝构件(Composite)角色:代表参加组合的有子对象的对象,并给出树枝构件对象的行为。
    */

    基础代码:

         // 创建一个树结构
                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);
                Composite compss = new Composite("Composite Y");
                compss.Add(new Leaf("Leaf XXB"));
                comp.Add(compss);
                root.Add(new Leaf("Leaf C"));
                // 添加和删除叶子
                Leaf l = new Leaf("Leaf D");
                root.Add(l);
                root.Remove(l);
    
                // 递归地显示节点
                root.Display(1);
    
                Console.WriteLine("
     ============ 
    ");
    
    
        /// <summary>
        /// 抽象构件(Component)角色
        /// </summary>
        public abstract class Component
        {
            protected string name; 
            // 构造函数
            public Component(string name)
            {
                this.name = name;
            }
            // 操作
            public abstract void Display(int depth);
        }
    
        //  树枝构件(Composite)角色
        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) + name); 
                // 显示每个节点的孩子
                foreach (Component component in children)
                    component.Display(depth + 3);
            }  
        }
        /// <summary>
        /// 树叶构件(Leaf)角色
        /// </summary>
        public class Leaf : Component
        {
            // 构造函数
            public Leaf(string name) : base(name) { } 
            // 从写函数
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
            }
        }
  • 相关阅读:
    SqlBulkCopy 类
    JavaScript的写类方式(2)——转
    asp.net在使用母版页的内容页面中动态加载css和js文件的方法
    .net数据绑定在HTML里有条件判断语句的写法
    c# winform实现网页上用户自动登陆,模拟网站登录
    DataSet DataTable获得总记录数
    DataSet,DataTable,DataReader,DataAdapter有什么联系
    asp.net的前台引用后台变量
    C#生成不重复随机数的两个函数
    SQL2008导出数据到远程数据库导致主键丢失、标识规范为否的解决办法
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3790611.html
Copyright © 2011-2022 走看看