C#設計模式-命令模式
阿新 • • 發佈:2018-12-18
using System; namespace TestCSharp { class Program { static void Main(string[] args) { Worker worker = new Soldier(); Command command = new Fight(worker); Officer officer = new General(command); officer.Execute(); Console.ReadKey(); } abstract class Command { Worker m_Worker; public Command(Worker worker) { m_Worker = worker; } public virtual void Execute() { m_Worker.Execute(this); } } abstract class Worker { public virtual void Execute(Command command) { } } abstract class Officer { Command m_Command; public Officer(Command command) { m_Command = command; } public virtual void Execute() { m_Command.Execute(); } } class Fight : Command { public Fight(Worker worker) : base(worker) { } } class General : Officer { public General(Command command) : base(command) { } } class Soldier : Worker { public override void Execute(Command command) { Console.WriteLine("Soldier Execute Command: " + command.GetType()); } } } }