首页 > 代码库 > DesignPattern_Behavioral_Command

DesignPattern_Behavioral_Command

void Main(){    Invoker invoker = new Invoker();    invoker.Add(new CommandA());    invoker.Add(new CommandB());    invoker.Notify();}class Receiver{    public void ShowA(){}    public void ShowB(){}}abstract class Command{    protected Receiver receiver = new Receiver();    public abstract void Show();}class CommandA:Command{    public override void Show(){        receiver.ShowA();    }}class CommandB:Command{    public override void Show(){        receiver.ShowB();    }}class Invoker{    List<Command> commands = new List<Command>();    public void Add(Command c){ commands.Add(c); }    public void Remove(Command c){ commands.Remove(c); }    public void Notify(){        foreach (var command in commands)        {            command.Show();        }    }}

 

DesignPattern_Behavioral_Command