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 = 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;
};
Tags:Javascript 乱弹 设计模式
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接