1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| var Human=function(param){ this.skill=param&¶m.skill||'保密'; this.hobby=param&¶m.hobby||'保密' }
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.splice(0,name.indexOf(' ')) that.SecondName=name.splice(name.indexOf(' ')) } })(name,that) }
var Work=function(work){ var that=this (function(work,that){ switch(work){ case 'code': that.work='工程师'; that.workDescript='每天沉迷于编程' break; case 'UI': case 'UE': that.work='设计师'; that.workDescript='设计是一种艺术' break; case 'teacher': that.work='教师'; that.workDescript='分享是一种快乐' break; default: that.work=work that.workDescript='对不起,无您选择的职位' } })(work,that) }
Work.prototype.changeWork=function(work){ this.work=work } Work.prototype.changeDescript=function(sentecnce){ this.workDescript=sentecnce }
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('xiao ming','code') console.log(person.skill) console.log(person.name.FirstName) console.log(person.work.work)
|
建造者模式和工厂模式的区别
工厂模式创建出来的是一个对象,它追求的是创建的结果,创建的过程不是重点,而建造者模式不仅可以得到创建的结果,也参与了创建的具体过程,对于创建的具体实现的细节也参与了干涉,创建的对象更复杂,这种模式创建的对象时一个复合对象