装饰者模式

2017-04-02  本文已影响0人  卡卡卡卡颂

用途

动态的给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象。

简单的例子

var Plane=function() {
  
}
Plane.prototype.fire=function() {
  console.log('fire!');
}

var MissileDecorator=function(plane) {
  this.plane=plane;
}
MissileDecorator.prototype.fire=function() {
  console.log('missile!!');
  this.plane.fire();
}

var plane=new Plane();
plane=new MissileDecorator(plane);
plane.fire();
上一篇 下一篇

猜你喜欢

热点阅读