zoukankan      html  css  js  c++  java
  • 【Unity与23种设计模式】命令模式(Command)

    GoF中定义:

    “将请求封装成为对象,让你可以将客户端的不同请求参数化,并配合队列、记录、复原等方法来执行请求的操作。”

    实现命令模式的标准在于:

    当请求被对象化后,对于请求对象是否有“管理”上的需求。如果有,则以命令模式实现。

    “管理”指的是对命令的延迟或命令的暂存。

    举个例子

    红警(红色警戒)中训练美国大兵的命令

    当点击时,开始倒计时产生大兵,再次点击时,显示数字“2”,表示要训练两个大兵

    也就是第二个命令被暂存

    当右键点击时,命令还可以撤销,数字由“2”变为了“1”,

    也就是第二个命令被撤销了

    命令的“暂存”和“撤销”用到的便是命令模式

    还有一种应用场景

    网游中,Client/Server间数据封包的传递,大多会使用命令模式来实现。

    //负责执行命令1
    public class Receiver1 {
        public Receiver1() { }
        public void Action(string Command) {
            Debug.Log("Receiver1.Action:Command["+Command+"]");
        }
    }
    
    //负责执行命令2
    public class Receiver2
    {
        public Receiver2() { }
        public void Action(int Param)
        {
            Debug.Log("Receiver2.Action:Command[" + Param.ToString() + "]");
        }
    }
    //执行命令的界面
    public abstract class Command {
        public abstract void Execute();
    }
    //将命令和Receiver绑定起来
    public class ConcreteCommand1 : Command {
        Receiver1 m_Receiver = null;
        string m_Command = "";
    
        public ConcreteCommand1(Receiver1 Receiver,string Command) {
            m_Receiver = Receiver;
            m_Command = Command;
        }
    
        public override void Execute()
        {
            m_Receiver.Action(m_Command);
        }
    }
    
    public class ConcreteCommand2 : Command
    {
        Receiver2 m_Receiver = null;
        int m_Param = 0;
    
        public ConcreteCommand2(Receiver2 Receiver, int Param)
        {
            m_Receiver = Receiver;
            m_Param = Param;
        }
    
        public override void Execute()
        {
            m_Receiver.Action(m_Param);
        }
    }
    //命令管理者
    public class Invoker {
        List<Command> m_Commands = new List<Command>();
    
        public void AddCommand(Command theCommand) {
            m_Commands.Add(theCommand);
        }
    
        public void ExecuteCommand() {
            foreach (Command theCommand in m_Commands) {
                theCommand.Execute();
            }
            m_Commands.Clear();
        }
    }
    //测试类
    public class TextCommand {
        void UnitText() {
            Invoker theInvoker = new Invoker();
    
            Command theCommand = null;
            theCommand = new ConcreteCommand1(new Receiver1(),"你好");
            theInvoker.AddCommand(theCommand);
            theCommand = new ConcreteCommand2(new Receiver2(),999);
            theInvoker.AddCommand(theCommand);
    
            theInvoker.ExecuteCommand();
        }
    }

    运行结果

    //Receiver.Action:Command[你好]
    //Receiver.Action:Param[999]

    还有一种应用场景

    网游中,Client/Server间数据封包的传递,大多会使用命令模式来实现。

    文章整理自书籍《设计模式与游戏完美开发》 菜升达 著

  • 相关阅读:
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31
    知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31无涯 - I
  • 原文地址:https://www.cnblogs.com/fws94/p/7443556.html
Copyright © 2011-2022 走看看