从代码角度来说,如果类型之间组成了层次结构,你希望使用统一的接口来管理每一个层次的类型的时候。
从应用角度来说,如果你希望把一对多的关系转化为一对一的关系的时候。
应用范围:
从代码角度来说,如果类型之间组成了层次结构,你希望使用统一的接口来管理每一个层次的类型的时候。
从应用角度来说,如果你希望把一对多的关系转化为一对一的关系的时候。
代码如下:
Composite pattern Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
namespace Composite_pattern
{
abstract class ABComposite
{
protected string name;
public ABComposite(string _name)
{
name = _name;
}
public abstract void Add(ABComposite a);
public abstract void Remove(ABComposite a);
public abstract void Star();
public abstract void Display();
public abstract void Stop();
}
class Composite : ABComposite
{
ArrayList arr = new ArrayList();
public Composite(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
arr.Add(a);
}
public override void Remove(ABComposite a)
{
arr.Remove(a);
}
public override void Display()
{
foreach(ABComposite a in arr)
{
a.Display();
}
}
public override void Star()
{
foreach(ABComposite ab in arr)
{
ab.Star();
}
}
public override void Stop()
{
foreach (ABComposite ab in arr)
{
ab.Stop();
}
}
}
class ComponentA:ABComposite
{
public ComponentA(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
throw new ApplicationException("you can't add elements with this object");
}
public override void Remove(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Display()
{
Console.WriteLine("i come from ComponentA");
}
public override void Star()
{
Console.WriteLine("ComponentA is stared");
}
public override void Stop()
{
Console.WriteLine("ComponentA is stopped");
}
}
class ComponentB:ABComposite
{
public ComponentB(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Remove(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Display()
{
Console.WriteLine("i come from ComponentB");
}
public override void Star()
{
Console.WriteLine("ComponentB is stared");
}
public override void Stop()
{
Console.WriteLine("ComponentB is stopped");
}
}
class Program
{
static void Main(string[] args)
{
Composite c = new Composite("composite");
ComponentA a=new ComponentA("hello a");
c.Add(new ComponentA("hello one"));
c.Add(new ComponentB("hhello two"));
c.Add(new ComponentB("hello tree"));
c.Add(new ComponentA("hello four"));
c.Add(a);
c.Star();
c.Display();
c.Remove(a);
Console.WriteLine("after removed !");
c.Display();
c.Stop();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
namespace Composite_pattern
{
abstract class ABComposite
{
protected string name;
public ABComposite(string _name)
{
name = _name;
}
public abstract void Add(ABComposite a);
public abstract void Remove(ABComposite a);
public abstract void Star();
public abstract void Display();
public abstract void Stop();
}
class Composite : ABComposite
{
ArrayList arr = new ArrayList();
public Composite(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
arr.Add(a);
}
public override void Remove(ABComposite a)
{
arr.Remove(a);
}
public override void Display()
{
foreach(ABComposite a in arr)
{
a.Display();
}
}
public override void Star()
{
foreach(ABComposite ab in arr)
{
ab.Star();
}
}
public override void Stop()
{
foreach (ABComposite ab in arr)
{
ab.Stop();
}
}
}
class ComponentA:ABComposite
{
public ComponentA(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
throw new ApplicationException("you can't add elements with this object");
}
public override void Remove(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Display()
{
Console.WriteLine("i come from ComponentA");
}
public override void Star()
{
Console.WriteLine("ComponentA is stared");
}
public override void Stop()
{
Console.WriteLine("ComponentA is stopped");
}
}
class ComponentB:ABComposite
{
public ComponentB(string name)
: base(name)
{ }
public override void Add(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Remove(ABComposite a)
{
throw new ApplicationException("you can't remove elements with this object");
}
public override void Display()
{
Console.WriteLine("i come from ComponentB");
}
public override void Star()
{
Console.WriteLine("ComponentB is stared");
}
public override void Stop()
{
Console.WriteLine("ComponentB is stopped");
}
}
class Program
{
static void Main(string[] args)
{
Composite c = new Composite("composite");
ComponentA a=new ComponentA("hello a");
c.Add(new ComponentA("hello one"));
c.Add(new ComponentB("hhello two"));
c.Add(new ComponentB("hello tree"));
c.Add(new ComponentA("hello four"));
c.Add(a);
c.Star();
c.Display();
c.Remove(a);
Console.WriteLine("after removed !");
c.Display();
c.Stop();
Console.ReadLine();
}
}
}