合成模式的应用

using System;
 using System.Collections;
using System.Windows.Forms;

namespace WindowsApplicationTemp
{
 
  

   // "Component"
   public abstract class Component
   {
       protected string name;
       public Component( string name )
       { this.name = name; }
       public string getName()
       { return name;
       }
       abstract public void Add(Component c);
       abstract public void Remove( Component c );
       abstract public IEnumerator getSubordinates();
   }

   // "Composite"
   public class Composite : Component
   {
       private ArrayList children = new ArrayList();
       public Composite( string name ) : base( name ) {}
       public override void Add( Component component )
       { children.Add( component ); }
 
       public override void Remove( Component component )
        { children.Remove( component ); }
       
        public override IEnumerator getSubordinates()
        {
            return children.GetEnumerator ();
        }
    }
 
    // "Leaf"
    public class Leaf : Component
    {
        private ArrayList childLeaf = new ArrayList(1);
        // Constructors
        public Leaf( string name ) : base( name ) {}
 
        // Methods
        public override void Add( Component c )
        { throw new Exception("not added!"); }

       public override void Remove( Component c )
       { throw new Exception("not removed!"); }
       public override IEnumerator getSubordinates()
       {
           return childLeaf.GetEnumerator();
       }
   }
  
   public class DisplayNode:TreeNode    
   {
       private Component cmpp;
       public DisplayNode(Component cmp )
       {
           cmp.getName ();
           cmpp = cmp;   
           this.Text=cmp.getName ();
       }

   }

 

 

 

 

    /// // "Component"
    public abstract class ComponentSafe
    {
        protected string name;
        public ComponentSafe( string name )
        { this.name = name; }
        public string getName()
        {
                return name;
        }
      abstract public IEnumerator getSubordinates();
       
    }

    // "Composite"
    public class CompositeSafe : ComponentSafe
    {
        private ArrayList children = new ArrayList();
        public CompositeSafe( string name ) : base( name ) {}
        public  void Add( ComponentSafe component )
        { children.Add( component ); }
  
        public  void Remove( ComponentSafe component )
        { children.Remove( component ); }
       
        public override IEnumerator getSubordinates()
        {
            return children.GetEnumerator ();
        }
    }
 
    // "Leaf"
    public class LeafSafe : ComponentSafe
    {
        private ArrayList childLeaf = new ArrayList(1);
        // Constructors
        public LeafSafe( string name ) : base( name ) {}
        public override IEnumerator getSubordinates()
        {
            return childLeaf.GetEnumerator();
        }
    }
   
    public class DisplayNodeSafe:TreeNode    
    {
        private ComponentSafe cmpp;
        public DisplayNodeSafe(ComponentSafe cmp )
        {
            cmp.getName ();
            cmpp = cmp;   
            this.Text=cmp.getName ();
        }

    }

   
}

 

//////////////////////////////////////////////////////////////

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplicationTemp
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;


  Component prez,pxi1,pxi2,pjy1,pjy2,pjy3,pjy4,pjs1,pjs2,pjs3,pjs4,pjs5,pjs6,pjs7;
  private System.Windows.Forms.TreeView TreeView1;

 

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //

   buildDisplayList();
          buildTree();

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.TreeView1 = new System.Windows.Forms.TreeView();
   this.SuspendLayout();
   //
   // TreeView1
   //
   this.TreeView1.ImageIndex = -1;
   this.TreeView1.Location = new System.Drawing.Point(72, 16);
   this.TreeView1.Name = "TreeView1";
   this.TreeView1.SelectedImageIndex = -1;
   this.TreeView1.Size = new System.Drawing.Size(360, 312);
   this.TreeView1.TabIndex = 0;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(592, 365);
   this.Controls.Add(this.TreeView1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  

 
  private void buildDisplayList()
         
 {
             prez = new Composite("南京铁道职业技术学院");
             pxi1=new Composite("通信工程系");
             pxi2=new Composite("计算机系");
     prez.Add(pxi1);
     prez.Add(pxi2);

             pjy1=new Composite("网络教研室");
             pjy2=new Composite("通讯教研室");
             pjy3=new Composite("移动通信教研室");
             pxi1.Add(pjy1);
             pxi1.Add(pjy2);
             pxi1.Add(pjy3);
             pjy4=new Composite("软件");
             pxi2.Add(pjy4);
             pjs1=new Leaf("沈瑞琴");
             pjy1.Add(pjs1);
             pjs2=new Leaf("晏荣");
             pjy1.Add(pjs2);
             pjs3=new Leaf("蒋明华");
             pjy1.Add(pjs3);
             pjs4=new Leaf("赵丽花");
             pjy1.Add(pjs4);
             pjs5=new Leaf("康瑞峰");
             pjy1.Add(pjs5);
             pjs6=new Leaf("冯明兵");
             pjy1.Add(pjs6);
             pjs7=new Leaf("刘伟");
             pjy1.Add(pjs7);
                    
         }
        private void buildTree()
       
   {
             DisplayNode nod;
 
             nod = new DisplayNode(prez);
             TreeView1.Nodes.Add(nod);
             addNodes(nod, prez);
         }
         private void addNodes(DisplayNode nod, Component cmp)
        
{
            Component newCmp;
            DisplayNode newNode;
            IEnumerator cmpEnum;
            cmpEnum = cmp.getSubordinates();
 
            while (cmpEnum.MoveNext())
           
{
                newCmp = (Component)cmpEnum.Current;
                newNode = new DisplayNode(newCmp);
                nod.Nodes.Add(newNode);
                addNodes(newNode, newCmp);
            }
        }

 
  
 }
}

///////////////////////////////////////////////////

CompositeSafe root;
        private void buildDisplayList()
       
{
            root = new CompositeSafe("南京铁道职业技术学院");
          CompositeSafe rootxi=new CompositeSafe("通信工程系");
          root.Add(rootxi);
          CompositeSafe rootxi1=new CompositeSafe("计算机系");
          root.Add(rootxi1);
           CompositeSafe rootjywl=new CompositeSafe("网络教研室");
           CompositeSafe rootjytx=new CompositeSafe("通讯教研室");
           CompositeSafe rootjyyd=new CompositeSafe("移动通信教研室");
           rootxi.Add(rootjywl);
           rootxi.Add(rootjytx);
           rootxi.Add(rootjyyd);
          
           CompositeSafe rootjyrj=new CompositeSafe("软件");
           rootxi1.Add(rootjyrj);
          
           rootjywl.Add(new LeafSafe("沈瑞琴"));
           rootjywl.Add(new LeafSafe("晏荣"));
           rootjywl.Add(new LeafSafe("蒋明华"));
           rootjywl.Add(new LeafSafe("赵丽花"));
           rootjywl.Add(new LeafSafe("康瑞峰"));
           rootjywl.Add(new LeafSafe("冯明兵"));
           rootjywl.Add(new LeafSafe("刘伟"));
          
                  
       }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值