• 组合模式


    <button id="button">按我</button>
        <script>
          var MacroCommand = function () {
            return {
              commandsList: [],
              add: function (command) {
                console.log(command);
    
                this.commandsList.push(command);
              },
              execute: function () {
                for (var i = 0, command; (command = this.commandsList[i++]); ) {
                  command.execute();
                }
              },
            };
          };
          var openAcCommand = {
            execute: function () {
              console.log("打开空调");
            },
          };
          /**********家里的电视和音响是连接在一起的,所以可以用一个宏命令来组合打开电视和打开音响的命令
           *********/
          var openTvCommand = {
            execute: function () {
              console.log("打开电视");
            },
          };
          var openSoundCommand = {
            execute: function () {
              console.log("打开音响");
            },
          };
          var macroCommand1 = MacroCommand();
          macroCommand1.add(openTvCommand);
          macroCommand1.add(openSoundCommand);
          /*********关门、打开电脑和打登录 QQ 的命令****************/
          var closeDoorCommand = {
            execute: function () {
              console.log("关门");
            },
          };
          var openPcCommand = {
            execute: function () {
              console.log("开电脑");
            },
          };
          var openQQCommand = {
            execute: function () {
              console.log("登录 QQ");
            },
          };
          var macroCommand2 = MacroCommand();
          macroCommand2.add(closeDoorCommand);
          macroCommand2.add(openPcCommand);
          macroCommand2.add(openQQCommand);
    
          /*********现在把所有的命令组合成一个“超级命令”**********/
          var macroCommand = MacroCommand();
          macroCommand.add(openAcCommand);
          macroCommand.add(macroCommand1);
          macroCommand.add(macroCommand2);
          /*********最后给遥控器绑定“超级命令”**********/
          var setCommand = (function (command) {
            document.getElementById("button").onclick = function () {
              command.execute();
            };
          })(macroCommand);
        </script>
  • 相关阅读:
    C#线程锁使用全功略
    viewstate 与 session 区别
    Server.MapPath() 用法
    SQL Server 存储过程
    数据库索引的概念
    从C#程序中调用非受管DLLs
    [转载]C++、C#写的WebService相互调用
    解决WCF接口无法传递object参数的问题
    UTF-8,UTF-16
    js 验证字符串是否全为中文
  • 原文地址:https://www.cnblogs.com/TTblog5/p/13162016.html
Copyright © 2020-2023  润新知