使用MDI表单实现命令模式

时间:2016-06-22 10:34:11

标签: c# command-pattern

我需要一些建议。我只是想了解设计模式。 我尝试过搜索和阅读,但我感到迷茫。 我真的希望你能给我一个建议。

这里我尝试附上一些图片和我的代码:

  1. MDI Form

    private void ItemAddNew_Click(object sender, EventArgs e) {
        InvokeDataCommand.Instance().SetCommand(new CommandDataManipulation(ActiveMdiChild as IDataManipulation, DataManipulations.Append));
        InvokeDataCommand.Instance().ExecuteCommand();
    }
    
  2. 接口

     public interface ICommand {
        void Execute();
     }
    
     public interface IDataManipulation {
        void Append();
        void Modify();
        void Remove();
     }
    
     public interface IDataReload {
        void Reload();
     }
    
  3. 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();
        }
    }
    
  4. 调用程序

     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();
        }
     }
    
  5. 接收机

    public partial class Display : Form, IDataManipulation, IDataReload { 
      ...
    }
    
  6. 我觉得除了使用命令我还使用单身。

    我现在做了什么是正确的?

0 个答案:

没有答案