// 创建人类类
var Human = function(param) {
this.skill = param && param.skill || '保密';
this.hobyy = param && param || '保密';
}
Human.prototype = {
getSkill : function() {
return this.skill;
},
getHobby : function() {
return this.hobby;
}
}
// 实例化姓名类
var Named = function(name) {
var that = this;
(function(name, that) {
that.wholeName = name;
if (name.indexOf(' ') > -1) {
that.firstName = name.slice(0, name.indexOf(' '));
that.secondName = name.slice(name.indexOf(' '));
}
})(name, that);
}
// 实例化职位
var Work = fucntion(work) {
var that = this;
(function(work, that) {
switch(work) {
case 'code':
that.work = '程序员';
that.workDescript = '撸一万行代码';
break;
case 'book':
that.work = '媒体从业者';
that.workDescript = '下笔如有神';
break;
case 'teach':
that.work = '教授';
that.workDescript = '百年育人';
break;
default :
that.work = work;
thar.workDescript = '对不起,我们还不清楚您职位的相关描述';
}
})(work, that);
}
Work.prototype.changeWork = function(work) {
this.work = work;
}
Work.prototype.changeDescript = function(text) {
this.workDescript = text;
}
/***
* 应聘者建造者
* 参数 name : 姓名
* 参数 work : 职位
**/
var Person = function(name, work) {
var _person = new Human();
_person.name = new Named(name);
_person.work = new Work(work);
return _person;
}
// 实例测试
var person = new Person('zhang san', 'code');
console.log(person.skill);
console.log(person.name.firstName);
console.log(person.work.work);
console.log(person.work.workDescript);
person.work.changeDescript('更改后的描述是:你好!');
console.log(person.work.workDescript);
建造者模式
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Definition Separate the construction of a complex object ...
- 单例模式(singleton) 保证一个类只有一个实例存在,提供对该实例加以访问的全局访问方法 实现: 构造函数私...
- 场景 当前有一套试题,总共有三道题目。现在小丽和亮亮要来答题。2B铅笔的内芯是什么材质? a.铅 b.碳 c.石墨...
- 本文大部分内容翻译至《Pro Design Pattern In Swift》By Adam Freeman,一...