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.

  • 相关阅读:
    Java基础系列——IO流
    如何为网站添加 CSS 暗模式(转载)
    详解 UWP (通用 Windows 平台) 中的两种 HttpClient API
    微软微服务架构 eShopOnContainers
    web开发中使用html/css全屏铺开显示
    web开发中的Cookie
    WPF依赖属性Binding实现
    SQL Server2014安装流程及注意事项
    .Net配置文件读取及修改方法封装(未加密)
    WCF开发优秀博客园推荐
  • 原文地址:https://www.cnblogs.com/cqxhl/p/6097438.html
Copyright © 2011-2022 走看看