正常来说命令应该长这样
public interface Command
{
void Execute();
}
public class Command_1 : Command
{
public Receiver_1 receiver;
public string command;
public Command_1(Receiver_1 receiver, string command)
{
this.receiver = receiver;
this.command = command;
}
public void Execute()
{
receiver.Action(command);
}
}
public class Receiver_1
{
public void Action(string command)
{
//这里放实作
Console.WriteLine(command);
}
}
==============================================================================
但我的疑问是“直接继承Command然后实作”不就好了?
像这样
public class Command_2 : Command
{
private int command;
public Command_2(int command)
{
this.command = command;
}
public void Execute()
{
Console.WriteLine(command);
}
}
是因为实作上比较不直觉吗?
==============================================================================
有查到一个教学https://goo.gl/mAqyG5
看到他把Receiver里面实作各种方法
看起来满方便的,但这样违反开放封闭原则吧?
所以现在已经有点搞不太懂命令模式为何要Command里面包Receiver了
而且C#直接用Action好像还比较方便?