我需要一些建议。我只是想了解设计模式。 我尝试过搜索和阅读,但我感到迷茫。 我真的希望你能给我一个建议。
这里我尝试附上一些图片和我的代码:
private void ItemAddNew_Click(object sender, EventArgs e) {
InvokeDataCommand.Instance().SetCommand(new CommandDataManipulation(ActiveMdiChild as IDataManipulation, DataManipulations.Append));
InvokeDataCommand.Instance().ExecuteCommand();
}
接口
public interface ICommand {
void Execute();
}
public interface IDataManipulation {
void Append();
void Modify();
void Remove();
}
public interface IDataReload {
void Reload();
}
Concreate Object
public class CommandDataManipulation : ICommand {
private IDataManipulation __receiver;
private DataManipulations __action;
public CommandDataManipulation(IDataManipulation receiver, DataManipulations action) {
__receiver = receiver ;
__action = action;
}
public void Execute() {
if (__receiver == null) return;
if (__action == DataManipulations.Append) __receiver.Append();
if (__action == DataManipulations.Modify) __receiver.Modify();
if (__action == DataManipulations.Remove) __receiver.Remove();
}
}
public class CommandDataReload : ICommand {
private IDataReload __receiver;
public CommandDataReload(IDataReload receiver) {
__receiver = receiver ;
}
public void Execute() {
if (__receiver == null) return;
__receiver.Reload();
}
}
调用程序
public class InvokeDataCommand {
private static InvokeDataCommand __instance;
private ICommand __command;
private InvokeDataCommand() {
__instance = null;
}
public static InvokeDataCommand Instance() {
if (__instance == null) {
__instance = new InvokeDataCommand();
}
return __instance;
}
public void SetCommand(ICommand command) {
__command = command;
}
public void ExecuteCommand() {
__command.Execute();
}
}
接收机
public partial class Display : Form, IDataManipulation, IDataReload {
...
}
我觉得除了使用命令我还使用单身。
我现在做了什么是正确的?