倪彼情感
您的当前位置:首页js装饰设计模式学习心得

js装饰设计模式学习心得

来源:倪彼情感


结构图:

接口

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

对象类

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;
 }
显示全文