建造者模式
建造者模式
建造者模式主要是将构建层与表示层分离,一种模块化的思想,使单个模块可以高效灵活的复用,但这种拆分同样增加了一定复杂度
实例:招聘简历
创建一个人类的类
const Human = function(param){ this.skill = param && param.skill || "保密" this.hobby = param && param.hobby || "保密" } Human.prototype = { getSkill: function(){ return this.skill }, getHobby: function(){ return this.hobby } }
创建一个姓名的类
const Named = function(name){ ((name)=>{ this.wholeName = name if(name.indexOf(" ") > -1){ this.FirstName = name.slice(0, name.indexOf(" ")) this.LastName = name.slice(name.indexOf(" ")) } })(name) }
创建一个职位的类
const Work = function(work){ ((work)=>{ switch(work){ case "code": this.work = "工程师" this.workDescript = "description" case "UE": this.work = "设计师" this.workDescript = "description" case "teach": this.work = "教师" this.workDescript = "description" default: this.work = work this.workDescript = "description" } })(work) } // 更换期待的职位 Work.prototype.changeWork = function(work){ this.work = work } // 更换描述 Work.prototype.changeDescript = function(descript){ this.workDescript = descript }
创建应聘者类
const Person = function(name, work){ const _person = new Human() _person.name = new Named(name) _person.work = new Work(work) return _person }