建造者模式
2016-04-12 本文已影响0人
niumew
// 创建人类类
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);