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());
}
}
}
}
C#设计模式-命令模式
猜你喜欢
转载自blog.csdn.net/qq_37273889/article/details/85046933
今日推荐
周排行