正常來說命令應該長這樣
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好像還比較方便?