12.01 - structure of the decorator.js

来自「JS设计模式源代码」· JavaScript 代码 · 共 93 行

JS
93
字号
/* The Bicycle interface. */var Bicycle = new Interface('Bicycle', ['assemble', 'wash', 'ride', 'repair',     'getPrice']);/* The AcmeComfortCruiser class. */var AcmeComfortCruiser = function() { // implements Bicycle  ...};AcmeComfortCruiser.prototype = {  assemble: function() {    ...  },  wash: function() {    ...  },  ride: function() {    ...  },  repair: function() {    ...  },  getPrice: function() {    return 399.00;  }};/* The BicycleDecorator abstract decorator class. */var BicycleDecorator = function(bicycle) { // implements Bicycle  Interface.ensureImplements(bicycle, Bicycle);  this.bicycle = bicycle;}BicycleDecorator.prototype = {  assemble: function() {    return this.bicycle.assemble();  },  wash: function() {    return this.bicycle.wash();  },  ride: function() {    return this.bicycle.ride();  },  repair: function() {    return this.bicycle.repair();  },  getPrice: function() {    return this.bicycle.getPrice();  }};/* HeadlightDecorator class. */var HeadlightDecorator = function(bicycle) { // implements Bicycle  this.superclass.constructor(bicycle); // Call the superclass's constructor.}extend(HeadlightDecorator, BicycleDecorator); // Extend the superclass.HeadlightDecorator.prototype.assemble = function() {  return this.bicycle.assemble() + ' Attach headlight to handlebars.';};HeadlightDecorator.prototype.getPrice = function() {  return this.bicycle.getPrice() + 15.00;};/* TaillightDecorator class. */var TaillightDecorator = function(bicycle) { // implements Bicycle  this.superclass.constructor(bicycle); // Call the superclass's constructor.}extend(TaillightDecorator, BicycleDecorator); // Extend the superclass.TaillightDecorator.prototype.assemble = function() {  return this.bicycle.assemble() + ' Attach taillight to the seat post.';};TaillightDecorator.prototype.getPrice = function() {  return this.bicycle.getPrice() + 9.00;};/* Usage. */var myBicycle = new AcmeComfortCruiser(); // Instantiate the bicycle.alert(myBicycle.getPrice()); // Returns 399.00myBicycle = new TaillightDecorator(myBicycle); // Decorate the bicycle object                                               // with a taillight.alert(myBicycle.getPrice()); // Now returns 408.00myBicycle = new HeadlightDecorator(myBicycle); // Decorate the bicycle object                                               // again, now with a headlight.alert(myBicycle.getPrice()); // Now returns 423.00

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?