合成模式的应用
2007-01-05 13:28
169 查看
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("刘伟"));
}
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("刘伟"));
}
相关文章推荐
- 设计模式的应用-策略模式实现支付方式回调策略
- 单例模式应用之聊天界面
- erlang应用行为模式
- 中移动试水SaaS管理模式 推动国内SaaS应用发展
- 黑马程序员------Java中单例模式的见解及其应用
- 设计模式 单例模式的缺陷和补救办法及应用场景2
- Web应用的缓存设计模式
- Activity的四种启动模式和应用场景
- Soap技术在Windows应用程序开发中的应用B/S与C/S混合模式开发[草稿]
- MVP模式在Android开发中的应用
- IOS 应用中从竖屏模式强制转换为横屏模式
- Activity的启动模式及应用实例
- TimesTen 应用层数据库缓存学习:19. 理解AWT缓存组的三种模式
- 浅谈移动应用的广告模式
- Lvs之NAT、DR、TUN三种模式的应用配置案例
- 18函数对象&19command模式20函数对象在STL中的应用
- pthread_once 之应用单例模式
- android应用开发设计模式之代理模式
- js中观察者模式的应用
- RAID 的常用模式 及mdadm 命令的应用