zoukankan      html  css  js  c++  java
  • 组合模式

    <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>
  • 相关阅读:
    基于注解的mybatis(转)
    git分支删除
    java多线程同步(转)
    hadoop学习笔记(五):java api 操作hdfs
    java常用设计模式一:单例模式
    mysql CONCAT用法
    mysql date_sub用法
    hadoop学习笔记(四):hdfs常用命令
    try-catch+thows异常范围说明
    Python 类的多态
  • 原文地址:https://www.cnblogs.com/TTblog5/p/13162016.html
Copyright © 2011-2022 走看看