public interface IAbstractCommand { void Execute(); }
public class ConcreteCommand1 : IAbstractCommand { private CompositeReceiver receiver; public ConcreteCommand1() { receiver=new CompositeReceiver(); } public void Execute() { receiver.Action1(); } }
public class ConcreteCommand2 : IAbstractCommand { private CompositeReceiver receiver; public ConcreteCommand2() { receiver=new CompositeReceiver(); } public void Execute() { receiver.Action2(); } }
public class CompositeInvoker : IAbstractCommand { private List<IAbstractCommand> children = new List<IAbstractCommand>(); public void Add(IAbstractCommand c) { children.Add(c); } public void Remove(IAbstractCommand c) { children.Remove(c); } public IAbstractCommand GetChild(int i) { return children[i]; } public void Execute() { foreach (var child in children) { child.Execute(); } } }
public class CompositeReceiver { public void Action1() { Console.WriteLine("接收者的Action1()方法被调用..."); } public void Action2() { Console.WriteLine("接收者的Action2()方法被调用..."); } }