【设计模式】组合模式和(宏)命令模式

组合模式

组合模式在对象间形成树形结构;组合模式中基本对象和组合对象被一致对待:无须关心对象有多少层,调用时只需在根部进行调用。

它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

js 复制代码
// 组合模式
const Folder = function(folderName) {
  this.folderName = folderName;
  this.files = [];
};

Folder.prototype.add = function(file) {
  this.files.push(file);
};

Folder.prototype.scan = function() {
  console.log(`开始扫描文件夹:${this.folderName}`);
  for (let i = 0; i < this.files.length; i++) {
    this.files[i].scan();
  }
};

const File = function(fileName) {
  this.fileName = fileName;
};

File.prototype.add = function() {
  throw new Error('文件下不能添加文件');
};

File.prototype.scan = function () {
  console.log(`开始扫描文件:${this.fileName}`);
}

// 测试
const folder = new Folder('文件夹');
const file1 = new File('文件1');
const file2 = new File('文件2');
folder.add(file1);
folder.add(file2);
folder.scan();
/*
开始扫描文件夹:文件夹
开始扫描文件:文件1
开始扫描文件:文件2
 */

这里是使用了 扫描文件的示例,还有一个应用场景是 动态声明 menu 菜单栏。

命令模式

有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么。需要一

种松耦合的方式来设计程序,使得发送者和接收者能够消除彼此,之间的耦合关系。

命令模式由三种角色构成:

  1. 发布者invoker(发出命令,调用命令对象,不知道如何执行与谁执行);
  2. 接收者receiver(提供对应接口处理请求,不知道谁发起请求),
  3. 命令对象command(接收命令,调用接收者对应接口处理发布者的请求)
js 复制代码
// 命令模式
class Receiver {
  execute() {
    console.log('接收者执行命令');
  }
}
class Command {
  constructor(receiver) {
    this.receiver = receiver;
  }
  execute() {
      console.log('命令对象->接收者->对应接口执行');
    this.receiver.execute();
  }
}
class Invoker {
  constructor(command) {
    this.command = command;
  }
  execute() {
      console.log('调用者执行命令');
    this.command.execute();
  }
}

/**
 * 调用者执行命令
 * 命令对象->接收者->对应接口执行
 * 接收者执行命令
 */
const receiver = new Receiver();
const command = new Command(receiver);
const invoker = new Invoker(command);
invoker.execute();

宏命令模式

js 复制代码
// 宏命令模式
const MacroCommand = function () {
    return {
        commandsList: [],
        add: function (command) {
            this.commandsList.push(command);
        },
        execute: function () {
            for (let item of this.commandsList) {
                item.execute();
            }
        }
    }
}

const closeDoorCommand = {
    execute: function () {
        console.log('关门');
    }
}

const openPcCommand = {
    execute: function () {
        console.log('开电脑');
    }
}

const openQQCommand = {
    execute: function () {
        console.log('登录QQ');
    }
}

const macroCommand = MacroCommand();
macroCommand.add(closeDoorCommand);
macroCommand.add(openPcCommand);
macroCommand.add(openQQCommand);
macroCommand.execute();
相关推荐
小码过河.2 小时前
设计模式——组合模式
组合模式
「QT(C++)开发工程师」4 小时前
C++设计模式
开发语言·c++·设计模式
茶本无香4 小时前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式
漂洋过海的鱼儿16 小时前
设计模式——EIT构型(三)
java·网络·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十八)—— 安全模式
人工智能·安全·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十六)—— 资源感知优化设计模式
人工智能·设计模式·langchain
老蒋每日coding1 天前
AI Agent 设计模式系列(十七)—— 推理设计模式
人工智能·设计模式
冷崖1 天前
桥模式-结构型
c++·设计模式
连山齐名1 天前
设计模式之一——堵塞队列
设计模式
会员果汁1 天前
19.设计模式-命令模式
设计模式·命令模式