开发学院网页设计JavaScript Javascript乱弹设计模式系列(3) - 装饰者模式(De... 阅读

Javascript乱弹设计模式系列(3) - 装饰者模式(Decorator)

 2010-09-14 13:35:20 来源:WEB开发网   
核心提示: 3. 添加ConcreteDecorator.js的具体装饰者:ConcreteDecorator.js// 头像装饰者function HeadDecorator(person, model) {PersonDecorator.call(this, person);this.model

3. 添加ConcreteDecorator.js的具体装饰者:

ConcreteDecorator.js
// 头像装饰者
function HeadDecorator(person, model) {
  PersonDecorator.call(this, person);
  this.model = model;
}
inheritClass(HeadDecorator, PersonDecorator);
HeadDecorator.prototype.getData = function() {
  var index = this.person.head.lastIndexOf("_");
  if(index != -1)
    this.person.head = this.person.head.slice(0, index);
  this.person.head = this.person.head + "_" + this.model;
  return this.person;
};
// 上衣装饰者
function BodyDecorator(person, model) {
  PersonDecorator.call(this, person);
  this.model = model;
}
inheritClass(BodyDecorator, PersonDecorator);
BodyDecorator.prototype.getData = function() {
  var index = this.person.body.lastIndexOf("_");
  if(index != -1)
    this.person.body = this.person.body.slice(0, index);
  this.person.body = this.person.body + "_" + this.model;
  return this.person;
};
// 腰裤装饰者
function FootDecorator(person, model) {
  PersonDecorator.call(this, person);
  this.model = model;
}
inheritClass(FootDecorator, PersonDecorator);
FootDecorator.prototype.getData = function() {
  var index = this.person.foot.lastIndexOf("_");
  if(index != -1)
    this.person.foot = this.person.foot.slice(0, index);
  this.person.foot = this.person.foot + "_" + this.model;
  return this.person;
};
// 背景装饰者
function BackgroundDecorator(person, model) {
  PersonDecorator.call(this, person);
  this.model = model;
}
inheritClass(BackgroundDecorator, PersonDecorator);
BackgroundDecorator.prototype.getData = function() {
  this.person.background = "bg" + this.model;
  return this.person;
};

上一页  2 3 4 5 6 7 8  下一页

Tags:Javascript 乱弹 设计模式

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接