Composite模式的实现
public abstract class Component
{
public string name;
abstract public void add(Component com);
abstract public void remove(Component com);
abstract public Component get(int i);
abstract public Component find(string name);
}
{
public string name;
abstract public void add(Component com);
abstract public void remove(Component com);
abstract public Component get(int i);
abstract public Component find(string name);
}
public class Composite : Component
{
private ArrayList children;
public Composite(string name)
{
this.children = new ArrayList();
this.name = name;
}
public override void add(Component com)
{
this.children.Add(com);
}
public override void remove(Component com)
{
this.children.Remove(com);
}
public override Component get(int i)
{
return this.children[i] as Component;
}
public override Component find(string name)
{
{
private ArrayList children;
public Composite(string name)
{
this.children = new ArrayList();
this.name = name;
}
public override void add(Component com)
{
this.children.Add(com);
}
public override void remove(Component com)
{
this.children.Remove(com);
}
public override Component get(int i)
{
return this.children[i] as Component;
}
public override Component find(string name)
{
if (this.name == name)
{
return this;
}
else
{
for (int i = 0; i < this.children.Count; i++)
{
Component com = this.children[i] as Component;
{
return this;
}
else
{
for (int i = 0; i < this.children.Count; i++)
{
Component com = this.children[i] as Component;
Component findcom = com.find(name);
if (findcom != null)
{
return findcom;
}
if (findcom != null)
{
return findcom;
}
}
return null;
}
}
return null;
}
}
}
public class leaf : Component
{
public leaf(string name)
{
this.name = name;
}
#region
public override void add(Component com)
{
throw new Exception("The method or operation is not implemented.");
}
public override Component get(int i)
{
throw new Exception("The method or operation is not implemented.");
}
public override void remove(Component com)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
public override Component find(string name)
{
if (this.name == name)
{
return this;
}
else
{
return null;
}
}
}
public class leaf : Component
{
public leaf(string name)
{
this.name = name;
}
#region
public override void add(Component com)
{
throw new Exception("The method or operation is not implemented.");
}
public override Component get(int i)
{
throw new Exception("The method or operation is not implemented.");
}
public override void remove(Component com)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
public override Component find(string name)
{
if (this.name == name)
{
return this;
}
else
{
return null;
}
}
}