原型模式
桥接模式
组合模式
- vue里的vnode
享元模式
- 内存共享
- 相同数据,共享使用
策略模式
- 不同策略分开处理
- 避免用太多
if...else...或者switch...case```javascript class OrdinaryUser { buy() { console.log(‘普通用户 buy’) } } class MemberUser { buy() { console.log(‘会员用户 buy’) } } class VipUser { buy() { console.log(‘Vip用户 buy’) } }
const u1 = new OrdinaryUser() u1.buy() const u2 = new VipUser() u2.buy() const u3 = new MemberUser() u3.buy()
<a name="BmOhe"></a>#### 模板方法模式- 抽象父类,具体子类```javascriptclass Drink {init() {this.boilWater();this.brew();this.pourInCup();if (!this.customerWantsCondiments()) return;// 默认需要调料this.addCondiments();}boilWater() {console.log("把水煮沸");}brew() {throw new Error("子类必须重写 brew 方法");}pourInCup() {throw new Error("子类必须重写 pourInCup 方法");}addCondiments() {throw new Error("子类必须重写 addCondiments 方法");}// 此处为钩子函数customerWantsCondiments() {return true;}}class Coffee extends Drink {brew() {console.log("用沸水冲泡咖啡");}pourInCup() {console.log("把咖啡倒进杯子");}addCondiments() {console.log("加糖和牛奶");}customerWantsCondiments() {return window.confirm("请问需要调料吗?");}}const c = new Coffee()c.init()
职责链模式
- 一步操作分多个职责角色完成
- 将发起者和各个处理者隔离
- 例子:请假审批,js的链式操作,jquery的链式操作
``javascript class Action { constructor(name) { this.name = name this.nextAction = null } setNextAction(action) { this.nextAction = action } handle() { console.log(${this.name} 审批`); if(this.nextAction != null) {
} } }this.nextAction.handle()
const a1 = new Action(‘组长’) const a2 = new Action(‘经理’) const a3 = new Action(‘总监’) a1.setNextAction(a2) a2.setNextAction(a3) a1.handle()
<a name="jzvz6"></a>#### 命令模式- 发布者和接收者分开```javascriptclass Receiver {exec() {console.log('exec');}}class Command {constructor(receiver) {this.receiver = receiver}cmd() {console.log('触发命令');this.receiver.exec()}}class Invoker {constructor(command) {this.command = command}invoke() {console.log('start');this.command.cmd()}}// 士兵const soldier = new Receiver()// 小号手const trumpeter = new Command(soldier)// 将军const general = new Invoker(trumpeter)general.invoke()
- 应用场景
- 网页富文本操作,浏览器封装了一个命令对象(document.execCommand)
备忘录模式
- 随时记录一个状态的变化
- 随时恢复到之前的某个状态
- 编辑器 ```javascript class Memento { constructor(content) { this.content = content; } getContent() { return this.content; } } class CareTaker { constructor() { this.list = []; } add(memento) { this.list.push(memento); } get(index) { return this.list[index]; } }
class Editor { constructor() { this.content = null; } setContent(content) { this.content = content; } getContent() { return this.content; } saveContentToMemento() { return new Memento(this.content); } getContentFromMemento(memento) { this.content = memento.getContent(); } }
const editor = new Editor(); const careTaker = new CareTaker();
editor.setContent(“1111”); editor.setContent(“1212”); careTaker.add(editor.saveContentToMemento()); editor.setContent(“1515”); careTaker.add(editor.saveContentToMemento()); editor.setContent(“1818”);
console.log(editor.getContent()); editor.getContentFromMemento(careTaker.get(1)); console.log(editor.getContent()); editor.getContentFromMemento(careTaker.get(0)); console.log(editor.getContent()); ```
中介者模式
访问者模式
- 将数据操作和数据结构分离
解释器模式
- babel
