• 设计模式_命令模式


    命令模式

    介绍

    定义案例问题堆积在哪里解决办法

    行为形设计模式


    就是把 “发布命令 执行命令”细化为多个角色
    每个角色又能继续细化

    发布命令

    1 打印1-9

    a 打印A-G

    如果有更多的命令
    命令处理方式更加多样性 更复杂
    处理命令的顺序
    拆分角色:降低耦合度
    命令类(一个命令一个类)

    具体接收类(具体的处理命令 当前
    用静态方法代替)

    执行执行

    先进先执行
    新进后执行
    优先等级高的先执行
    )可以设置多种优先等级

    类图

    1 . 一个命令接口类

    2 “命令接口类” 包含了 “处理类”

    3 传给了“调用方” 来定义如何调用

    代码

    角色1  BaseCommand :抽象命令

    角色2.1 Command1 :具体命令1

    角色2.2 CommandA :具体命令2

    角色3 Receiver:具体命令处理

    角色4 Invoke:执行方

    BaseCommand

    1. public abstract class BaseCommand
    2. {
    3. // 委托:命令
    4. public delegate void ExecuteCommand();
    5. public ExecuteCommand executeCommand = null;
    6. public BaseCommand(ExecuteCommand executeCommand)
    7. {
    8. this.executeCommand += executeCommand;
    9. }
    10. // 执行命令
    11. public abstract void Execute();
    12. }

    Command1

    1. public class Command1 : BaseCommand
    2. {
    3. public Command1(ExecuteCommand executeCommand)
    4. : base(executeCommand)
    5. {
    6. }
    7. public override void Execute()
    8. {
    9. if (null != executeCommand)
    10. executeCommand();
    11. }
    12. }

    CommandA

    1. public class CommandA : BaseCommand
    2. {
    3. public CommandA(ExecuteCommand executeCommand)
    4. : base(executeCommand)
    5. {
    6. }
    7. public override void Execute()
    8. {
    9. if (null != executeCommand)
    10. executeCommand();
    11. }
    12. }

    Receiver

    1. using UnityEngine;
    2. ///
    3. /// 功能集合
    4. ///
    5. public class Receiver
    6. {
    7. static public void Show1to9()
    8. {
    9. Debug.Log("打印:123456789!");
    10. }
    11. static public void showAtoG()
    12. {
    13. Debug.Log("打印:ABCDEFG!");
    14. }
    15. }

    Invoke

    1. ///
    2. /// 调用者
    3. /// 可以继续扩展:
    4. /// 1 收集命令
    5. /// 2 命令顺序不同 倒序 或者 特殊优先级高的先执行
    6. /// 3 扩展为设计模式深入设计
    7. ///
    8. public class Invoke
    9. {
    10. private BaseCommand commend = null;
    11. Invoke() { }
    12. public Invoke(BaseCommand commend)
    13. {
    14. this.commend = commend;
    15. }
    16. public void Execute()
    17. {
    18. commend.Execute();
    19. }
    20. }

    运行代码

    1. using System;
    2. using UnityEngine;
    3. public class TestML : MonoBehaviour
    4. {
    5. void Start()
    6. {
    7. BaseCommand command = null;
    8. string strCommand = "1";
    9. switch (strCommand)
    10. {
    11. case "A":
    12. command = new CommandA(Receiver.showAtoG);
    13. break;
    14. case "1":
    15. command = new Command1(Receiver.Show1to9);
    16. break;
    17. default:
    18. break;
    19. }
    20. // 执行命令
    21. Invoke invoke = new Invoke(command);
    22. invoke.Execute();
    23. }
    24. }

    运行结果

    心得备注

    设计模式需要放到框架设计 才更有意义,有时候如果有一个小的需求并且后期也不会改动,直接用流程的方式写代码更加简单,进一步设计反而没必要!

    如果放入项目框架, 命令模式的4个角色还能继续细分,细分后再细分,考虑后期的各种变动,根据策划案进一步细分优化,细节处使用更多的设计模式。

    一步步优化下去, 推迟细节再推迟。。直到config配置文件或者Execl。

  • 相关阅读:
    如何借助问答平台上做好网络营销?
    springMVC中统一异常处理@ControllerAdvice
    【Kotlin 协程】协程底层实现 ④ ( 结构化并发 | viewModelScope 作用域示例 )
    使用HTTP爬虫ip中的常见误区与解决方法
    C#多线程学习(二) 如何操纵一个线程
    《数据库系统概论》:DBA的职责有些
    Linux之shell脚本编程、多命令、脚本、bc计算器、反引号、if语句、for语句
    Leetcode 1124. 表现良好的最长时间段
    分分钟让你学会栈和队列
    STM32定时器输入捕获测量高电平时间
  • 原文地址:https://blog.csdn.net/qq_30926011/article/details/133810996