Creative design pattern builder pattern
Definition
The so-called builder mode is the consideration of details. We need to consider the whole and details to build a qualified design. That's our class. The presentation layer and our infrastructure need to be separated.
case analysis
1. Applicants need to release resumes on our platform
2. Show the job applicant's specialty
3. Hide the personal privacy information and contact information of job seekers
let Person = function(param){
this.skill = param && param.skill || 'secrecy';
this.hobby = param && param.hobby || 'secrecy';
}
Person.prototype = {
getSkill(){
return this.skill
}
getHobby(){
return this.hobby;
}
}
let Named = function(name){
let that = this;
//constructor
//Constructor resolves last name and first name of a name
(function(that,name){
that.fullName = name;
if(name.indexOf(' ') !== -1){
that.firstName = name.slice(0,name.indexOf(' '));
that.lastName = name.slice(name.indexOf(' '))
}
})(that,name);
}
let Work = function(work){
var that = this;
(function(that,work){
switch(work){
case 'CODER':
that.work = 'software engineer';
that.workDescription = 'The world of knitting farmers'
break;
case 'UI':
that.work = 'Graphic Designer';
that.workDescription = 'Design is always so casual, but it's aesthetic';
break;
case 'UE':
that.work = 'Designer';
that.workDescription ='Take a different road and share the eternal beauty'
break;
default:
that.work = work;
that.workDescription = 'Sorry, we don't know the description of the position you chose'
break;
}
})(that,work)
}
Work.prototype = {
changeWork(work){
this.work = work;
}
changeDescription(description){
this.workDescription = description;
}
}
Create a job seeker
let JobSeeker =function(name,work){
// Candidates to cache
let _jobSeeker = new Person();
// Create job seeker name
_jobSeeker.name = new Named(name);
//Create a job seeker's desired position
_jobSeeker.work = new Work(work);
return _jobSeeker;
}
//test
let Anna = new JobSeeker('Anna Chen','UE');
console.log(Anna.name.firstName) // Anna;
console.log(Anna.work.workDescription) // Take a different road and share the eternal beauty
All the factory models will eventually get a whole.
Reference book: JavaScript design mode, Zhang Rongming
At the same time, you can also refer to this reader's impressions