建造者模式:將一個(gè)復(fù)雜對(duì)象的構(gòu)建層與其表示層分離史简,同樣的構(gòu)建過(guò)程,可采用不同的表示
function Person(param){
this.name = param && param.name || '保密';
this.age = param && param.age || '保密';
}
Person.prototype = {
getName : function(){
return this.name
}
}
function Skills(skill){
this.skill = skill || '' ;
}
Skills.prototype = {
getSkill : function(){
return this.skill
}
}
function National(national){
this.national = national || ''
}
National.prototype = {
getNational : function(){
return this.national;
}
}
var CreatePerson= function(param){
var man = new Person(param);
man.skills = new Skills(param.skill)
man.national = new National(param.national) ;
return man;
}
var newPerson = new CreatePerson({name:'jack',age:'20',skill:'web',national:'china'})
console.log(newPerson .skills.skill) //web
console.log(newPerson .national .national ) //china
建造者將一個(gè)完整的部分拆分成不同的類,然后通過(guò)拼接的方式組合成一個(gè)完整的部分威酒;
相應(yīng)的也更加關(guān)注于不同模塊本身的一些細(xì)節(jié)處理窑睁,而并非工廠那樣,關(guān)心最終的產(chǎn)物葵孤;