• 设计模式之命令模式


    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.

  • 相关阅读:
    python基础篇 08 文件操作
    python基础篇 07set集合 深浅拷贝
    python 基础篇 06 编码 以及小知识点补充
    python基础篇 05字典
    钉钉中设置代码提交提醒--Github机器人(转)
    Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)
    Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)
    为什么添加了@Aspect 还要加@Component(转)
    Servlet 服务器 HTTP 响应
    Servlet 客户端 HTTP 请求
  • 原文地址:https://www.cnblogs.com/cqxhl/p/6097438.html
Copyright © 2020-2023  润新知