UML图:
Composite抽象类接口如下,包含孩子节点,增加孩子节点的方法,删除孩子节点的方法,以及操作。
public abstract class Composite
{
protected string name;
protected ArrayList childs = new ArrayList();
public virtual void Add(Composite composite)
{
}
public virtual void Remove(Composite composite)
{
}
public virtual void Operation()
{
}
}
实现具体的Menu和MenuList,Menu包含MenuList,Menu相当于普通节点包含MenuList,而MenuList是叶子节点,没有任何孩子节点,以下是具体实现代码:
public class MenuItem : Composite
{
public MenuItem(string name)
{
this.name = name;
}
public override void Operation()
{
LandpyForm.Form.OutputResult("MenuItem" + name);
}
}
public class Menu : Composite
{
public Menu(string name)
{
this.name = name;
}
public override void Add(Composite composite)
{
childs.Add(composite);
}
public override void Remove(Composite composite)
{
childs.Remove(composite);
}
public IIterator CreateIterator()
{
return new ArrayListIterator(childs);
}
public override void Operation()
{
LandpyForm.Form.OutputResult("Menu" + name);
}
}
测试代码:
DotaPatternLibrary.Composite.Menu menu = new DotaPatternLibrary.Composite.Menu("root");
for (int i = 0; i < 5; i++)
{
DotaPatternLibrary.Composite.MenuItem menuItem = new DotaPatternLibrary.Composite.MenuItem("root:" + i.ToString());
if (i != 3)
{
menu.Add(menuItem);
}
else
{
DotaPatternLibrary.Composite.Menu menuNew = new DotaPatternLibrary.Composite.Menu("New");
for (int j = 0; j < 5; j++)
{
DotaPatternLibrary.Composite.MenuItem menuItemNew = new DotaPatternLibrary.Composite.MenuItem("New:" + j.ToString());
menuNew.Add(menuItemNew);
}
menu.Add(menuNew);
}
}
LandpyForm.Form.OutputResult("Menu Ready");
ShowMenu(menu);
以上是初始化菜单,以及ShowMenu方法实现Menu的遍历,详见完整代码。
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DotaCommon;
namespace DotaPatternLibrary.Composite
{
public interface IIterator
{
bool HasNext();
Composite Next();
}
internal class ArrayListIterator : IIterator
{
#region IIterator 成员
public bool HasNext()
{
if (position > arrayList.Count - 1 || arrayList[position] == null)
{
return false;
}
return true;
}
public Composite Next()
{
Composite returnValue = arrayList[position] as Composite;
position++;
return returnValue;
}
#endregion
private ArrayList arrayList;
private int position;
public ArrayListIterator(ArrayList arrayList)
{
this.arrayList = arrayList;
}
}
internal class ArrayIterator : IIterator
{
#region IIterator 成员
public bool HasNext()
{
if (position > array.Count - 1 || array[position] == null)
{
return false;
}
return true;
}
public Composite Next()
{
Composite returnValue = array[position] as Composite;
position++;
return returnValue;
}
#endregion
private IList array;
private int position;
public ArrayIterator(IList array)
{
this.array = array;
}
}
public abstract class Composite
{
protected string name;
protected ArrayList childs = new ArrayList();
public virtual void Add(Composite composite)
{
}
public virtual void Remove(Composite composite)
{
}
public virtual void Operation()
{
}
//public virtual ArrayList GetChild()
//{
// return null;
//}
}
public class MenuItem : Composite
{
public MenuItem(string name)
{
this.name = name;
}
public override void Operation()
{
LandpyForm.Form.OutputResult("MenuItem" + name);
}
}
public class Menu : Composite
{
public Menu(string name)
{
this.name = name;
}
public override void Add(Composite composite)
{
childs.Add(composite);
}
public override void Remove(Composite composite)
{
childs.Remove(composite);
}
//public override ArrayList GetChild()
//{
// return childs;
//}
public IIterator CreateIterator()
{
return new ArrayListIterator(childs);
}
public override void Operation()
{
LandpyForm.Form.OutputResult("Menu" + name);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DotaCommon;
namespace DotaPatternLibrary.Composite
{
public interface IIterator
{
bool HasNext();
Composite Next();
}
internal class ArrayListIterator : IIterator
{
#region IIterator 成员
public bool HasNext()
{
if (position > arrayList.Count - 1 || arrayList[position] == null)
{
return false;
}
return true;
}
public Composite Next()
{
Composite returnValue = arrayList[position] as Composite;
position++;
return returnValue;
}
#endregion
private ArrayList arrayList;
private int position;
public ArrayListIterator(ArrayList arrayList)
{
this.arrayList = arrayList;
}
}
internal class ArrayIterator : IIterator
{
#region IIterator 成员
public bool HasNext()
{
if (position > array.Count - 1 || array[position] == null)
{
return false;
}
return true;
}
public Composite Next()
{
Composite returnValue = array[position] as Composite;
position++;
return returnValue;
}
#endregion
private IList array;
private int position;
public ArrayIterator(IList array)
{
this.array = array;
}
}
public abstract class Composite
{
protected string name;
protected ArrayList childs = new ArrayList();
public virtual void Add(Composite composite)
{
}
public virtual void Remove(Composite composite)
{
}
public virtual void Operation()
{
}
//public virtual ArrayList GetChild()
//{
// return null;
//}
}
public class MenuItem : Composite
{
public MenuItem(string name)
{
this.name = name;
}
public override void Operation()
{
LandpyForm.Form.OutputResult("MenuItem" + name);
}
}
public class Menu : Composite
{
public Menu(string name)
{
this.name = name;
}
public override void Add(Composite composite)
{
childs.Add(composite);
}
public override void Remove(Composite composite)
{
childs.Remove(composite);
}
//public override ArrayList GetChild()
//{
// return childs;
//}
public IIterator CreateIterator()
{
return new ArrayListIterator(childs);
}
public override void Operation()
{
LandpyForm.Form.OutputResult("Menu" + name);
}
}
}