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

组合模式

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

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

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();
相关推荐
数据知道1 天前
Go语言设计模式:适配器模式详解
设计模式·golang·建造者模式
执笔论英雄1 天前
【设计模式】策略类和依赖注入
设计模式
郝学胜-神的一滴1 天前
深入解析C++命令模式:设计原理与实际应用
开发语言·c++·程序人生·软件工程·命令模式
手把手入门1 天前
23种设计模式
设计模式
qqxhb1 天前
系统架构设计师备考第59天——SOA原则&设计模式
设计模式·系统架构·版本管理·标准化·松耦合·可复用·服务粒度
Yeniden1 天前
【设计模式】桥接模式大白话讲解
设计模式·桥接模式
崎岖Qiu1 天前
【设计模式笔记10】:简单工厂模式示例
java·笔记·设计模式·简单工厂模式
数据知道2 天前
Go语言设计模式:工厂模式详解
开发语言·设计模式·golang·go语言·工厂模式
懒羊羊不懒@2 天前
JavaSe—泛型
java·开发语言·人工智能·windows·设计模式·1024程序员节
rookie_fly2 天前
基于Vue的数字输入框指令
前端·vue.js·设计模式