欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js裝飾設(shè)計(jì)模式學(xué)習(xí)心得

 更新時(shí)間:2018年02月17日 08:53:20   投稿:laozhang  
本片文章給大家分享一下作者學(xué)習(xí)Javascript裝飾設(shè)計(jì)模式后的心得以及要點(diǎn)分享,有興趣的朋友參考下。

裝飾設(shè)計(jì)模式

每種設(shè)都有其獨(dú)特的應(yīng)用場景和解決問題的方式, 裝飾設(shè)計(jì)模式是動(dòng)態(tài)的為對(duì)象添加新的功能, 是一種用于代替繼承的技術(shù),無需通過繼承增加子類就能擴(kuò)展對(duì)象的新功能。使用對(duì)象的關(guān)聯(lián)關(guān)系代替繼承關(guān)系,更加靈活,同時(shí)避免類型體系的快速膨脹, 這種模式適合新添加的功能不足以用繼承為代價(jià)解決問題的情況時(shí)使用 - 殺雞焉用宰牛刀 ^_^
裝飾設(shè)計(jì)模式: 動(dòng)態(tài)地為一個(gè)對(duì)象添加一些額外的職責(zé),若要擴(kuò)展一個(gè)對(duì)象的功能,裝飾者提供了比繼承更有彈性的替代方案。

結(jié)構(gòu)圖:

接口

var Bicycle = new Interface('Bicycle', ['assemble', 'wash', 'repair', 'getPrice']);

對(duì)象類

var AcmeComfortCuiser = function(){
  
};
AcmeComfortCuiser.prototype = {
  assemble: function(){
    
  },
  wash: function(){
    
  },
  repair: function(){
    
  },
  getPrice: function(){
    
  }
}

裝飾類

var BicycleDecorator = function(bicycle){
  Interface.ensureImplements(bicycle, Bicycle);
  this.bicycle = bicycle;
};
BicycleDecorator.prototype = {
  assemble: function(){
    return this.bicycle.assemble();
  },
  wash: function(){
    return this.bicycle.wash();
  },
  repair: function(){
    return this.bicycle.repair();
  },
  getPrice: function(){
    return this.bicycle.getPrice();
  }
}

拓展類

  var HeadlightDecorator = function(bicycle){
    BicycleDecorator.call(this, bicycle);
  };
  extend(HeadlightDecorator, BicycleDecorator);
  HeadlightDecorator.prototype.getPrice = function(){
    return this.bicycle.getPrice() + 15.00;
  }

相關(guān)文章

最新評(píng)論