其他设计模式

2019-06-21  本文已影响0人  EmilWong

一、原型模式

概念:clone自己,生成一个新对象;Java默认有clone接口,不用自己实现
UML类图
JS中的应用

// `Object.create` 用到了原型模式的思想(虽然不是Java中的clone)
// 基于一个原型创建一个对象
var prototype = {
  getName: function () {
    return this.first + '  ' + this.last
  },
  say: function () {
    console.log('hello')
  }
}
// 基于原型创建X
var x = Object.create(prototype)
x.first = 'A'
x.last = 'B'
console.log(x.getName())
x.say()

二、桥接模式

概念:用于把抽象画与实现化解耦,使得二者可以独立变化
UML类图


image.png

实现:

class Color {
  constructor(name) {
    this.name = name
  }
}
class Shape {
  constructor(name, color){
    this.name = name
    this.color = color
  }
  draw() {
    console.log(`${this.color.name} ${this.name}`)
  }
 }
// 测试代码
let red = new Color('red')
let yellow = new Color('yellow')
let circle = new Shape('circle', red)
circle.draw()
let triangle = new Shape('triangle', yellow)
triangle.draw()

设计原则验证

三、组合模式

概念:生成树形结构,表示"整体-部分"关系
JS应用:虚拟DOM中的VNode是这种形式,但数据类型简单

四、享元模式

五、策略模式

概念:

演示

class User {
  constructor (type) {
    this.type = type
  }
  buy() {
    if(this.type === 'ordinary') {
      console.log('普通用户购买')
    } else if(this.type === 'member') {
      console.log('会员用户购买')
    } else if (this.type === 'vip'){
      console.log('vip 用户购买')
    }
  }
}
// 测试代码
var u1 = new User('ordinary')
u1.buy()
var u2 = new User('member')
u2.buy()

// 策略模式改写
class OrdinaryUser{
  buy () {
    console.log('普通用户购买')
  }
}
class MemberUser {
  buy() {
    console.log('会员用户购买')
  }
}
class VipUser{
  buy(){
    console.log('vip用户购买')
  }
}
var u1 = new OrdinaryUser()
u1.buy()

六、模板方法模式

class Action {
handle() {
handle1()
handle2()
handle3()
}
handle1(){
console.log('1')
}
handle2(){
console.log('2')
}
handle3(){
console.log('3')
}
}

七、职责链模式

概念:

代码演示:

// 请假需要蹭蹭审批
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()
    }
  }
}
let a1 = new Action('组长')
let a2 = new Action('经理')
let a3 = new Action('总监')
a1.setNextAction(a2)
a2.setNextAction(a3)
a1.handle()

JS中的应用

八、命令模式

概念:

JS代码演示

// 接收者
class Receiver{
  exec() {
    console.log('执行')
  }
}
// 命令者
class Command {
  constructor(receiver){
    this.receiver = receiver
  }
  cmd() {
    console.log('触发命令')
    this.receiver.exec()
  }
}
// 触发者
class Invoker {
  constructor(command){
    this.command = command
  }
  invoke() {
    console.log('开始')
    this.command.cmd()
  }
}
// 士兵
let soldier = new Receiver()
// 小号手
let trumpeter = new Command(soldier)
let general = new Invoker(trumpeter)
general.invoke() 

JS中的应用

九、备忘录模式

概念:

JS代码演示

// 备忘对象
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()
  }
}
// 测试代码
let editor = new Editor()
let careTaker = new CareTaker()

editor.setContent('111')
editor.setContent('222')
careTaker.add(editor.saveContentToMemento()) // 将当前内容备份
editor.setContent('333')
careTaker.add(editor.saveContentToMemento()) // 将当前内容备份
editor.setContent('444')
console.log(editor.getContent()) // 444
editor.getContentFromMemento(careTaker.get(1))  // 撤销
console.log(editor.getContent()) // 333

设计原则验证

十、中介者模式

概念:对象和对象之间的访问都通过一个中介者

十一、访问者模式

概念:

十二、解释器模式

概念:

十三、关于面试

上一篇 下一篇

猜你喜欢

热点阅读