zoukankan      html  css  js  c++  java
  • 设计模式之命令模式

    1、类图

    命令模式结构:

    实例类图:

    2、创建项目

    …………………………

    3、 FunctionButton:功能键类,充当请求调用者(请求发送者)。

    using System;

    namespace CommandSample

    {

        class FunctionButton

        {

            private Command command;

            public Command Command

            {

                get { return command; }

                set { command = value; }

            }

            public void Click()

            {

                Console.WriteLine("单击功能键!");

                command.Execute();

            }

        }

    }

    2、  Command:抽象命令类

    namespace CommandSample

    {

        abstract class Command

        {

            public abstract void Execute();

        }

    }

    4、 ExitCommand:退出命令类,充当具体命令类

    namespace CommandSample

    {

        class ExitCommand : Command

        {

            private SystemExitClass seObj;

            public ExitCommand()

            {

                seObj = new SystemExitClass();

            }

            public override void Execute()

            {

                seObj.Exit();

            }

        }

    }

    5、 HelpCommand:帮助命令类,充当具体命令类。

    namespace CommandSample

    {

        class HelpCommand : Command

        {

            private DisplayHelpClass hcObj;

            public HelpCommand()

            {

                hcObj = new DisplayHelpClass();

            }

            public override void Execute()

            {

                hcObj.Display();

            }

        }

    }

    6、 SystemExitClass:退出系统模拟实现类,充当强求接收者。

    using System;

    namespace CommandSample

    {

        class SystemExitClass

        {

            public void Exit()

            {

                Console.WriteLine("退出系统!");

            }

        }

    }

    7、 DisplayHelpClass:显示帮助文档模实现类,充当请求接收者。

    using System;

    namespace CommandSample

    {

        class DisplayHelpClass

        {

            public void Display()

            {

                Console.WriteLine("显示帮助文档!");

            }

        }

    }

    8、 配置文件App.config:在配置文件中存储了具体命令类的类名。

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <appSettings>

        <add key="command" value="CommandSample.HelpCommand"/>

      </appSettings>

    </configuration>

    9、 Program:客户端测试类

    using System;

    using System.Configuration;

    using System.Reflection;

    namespace CommandSample

    {

        class Program

        {

            static void Main(string[] args)

            {

                FunctionButton fb = new FunctionButton();

                

                Command command;

                //读取配置文件

                string commandStr = ConfigurationManager.AppSettings["command"];

                //反射生成对象

                command = (Command)Assembly.Load("CommandSample").CreateInstance(commandStr);

                //设置命令对象

                fb.Command = command;

                fb.Click();

                Console.Read();

            }

        }

    }

    10、 结果及分析,编译并运行程序,输出结果如下:

     

    如果需要更换具体命令类,无须修改源代码,只需修改配置文件,例如将退出命令改为帮助命令,只需将存储在配置文件中的具体命令类名ExitCommand改为HelpCommand.

  • 相关阅读:
    Windows API的CreateFile()中的OPEN_ALWAYS和CREATE_ALWAYS之间的差异
    Linux下的虚拟串口对(可用于在本机上模拟串口进行调试)
    [转&精]IO_STACK_LOCATION与IRP的一点笔记
    【转】在WIN7、WIN10操作系统用WebDAV映射网络驱动器需要的操作
    Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information
    分布式处理大数据的目录及学习树
    利用docker将传统企业集中式架构转换为微服务架构的实践经验
    docker学习笔记5 端口映射与容器互联
    docker学习笔记4 数据管理、持久化
    docker学习笔记3 容器 container
  • 原文地址:https://www.cnblogs.com/cqxhl/p/6097438.html
Copyright © 2011-2022 走看看