Js Decoration Design Model learning experience, js Decoration Design learning experience
Decorative Design Mode
Each configuration has its own unique application scenario and solution to problems. The Decoration Design Mode dynamically adds new functions to objects and is a technology used to replace inheritance, new Functions of objects can be extended without adding subclass by inheritance. Replace the inheritance relationship with the association relationship of objects, which is more flexible and avoids the rapid expansion of the type system, this mode is applicable when the newly added features are insufficient to solve the problem at the cost of inheritance.-Use the cool-killing tool ^_^
Decoration Design Pattern: dynamically add some additional responsibilities to an object. To expand the functions of an object, the modifier provides an alternative solution that is more flexible than inheritance.
Structure:
Interface
var Bicycle = new Interface('Bicycle', ['assemble', 'wash', 'repair', 'getPrice']);
Object Class
var AcmeComfortCuiser = function(){ };AcmeComfortCuiser.prototype = { assemble: function(){ }, wash: function(){ }, repair: function(){ }, getPrice: function(){ }}
Decoration
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(); }}
Extension
var HeadlightDecorator = function(bicycle){ BicycleDecorator.call(this, bicycle); }; extend(HeadlightDecorator, BicycleDecorator); HeadlightDecorator.prototype.getPrice = function(){ return this.bicycle.getPrice() + 15.00; }