⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12.01 - structure of the decorator.js

📁 JS设计模式源代码
💻 JS
字号:
/* 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -