Builder model
By separating the construction layer of a complex object from its presentation layer, the same construction process can be represented differently.
The builder pattern is different from the factory method, which creates a class or a cluster of classes, and the builder pattern creates concrete objects through different representations.
/ / create a Human var Human = function (param) {this. Skill = param && param. Skill | | 'secret'; This. Hobby = param && param. Hobby | | 'secret'; } 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 = function (Work) {let that = this; (function (work, that) {switch (work) {case 'code': that. Work = 'engineer '; That. WorkDescript = 'get addicted to programming '; break; Case 'UI': case 'UE': that. Work = 'designer '; That. WorkDescript = 'Design is more like art '; break; Case 'teach': that. Work = 'teacher '; That. WorkDescript = 'Sharing is also a kind of happiness '; break; default: that.work = work; That. WorkDescript = 'Sorry, we are not clear about your choice of job description' break; (work, that)}}}) / / change the desired position work. The prototype. ChangeWork = function (work) {this. Work = work; } / / add descriptions of Work Work. The prototype. ChangeDescript = function (setence) {enclosing workDescript = setence; 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 (' code'); console.log(person.skill); / / confidentiality console. The log (person. Name. FirstName); / / small console. The log (person. Work. The work); // Engineer console.log(person.work.workdescript); / / indulge in programming the person every day. The work. ChangeDescript (' change my job description '); console.log(person.work.workDescript); // Change the job descriptionCopy the code
conclusion
The Builder pattern not only captures the results of the creation, but also participates in the creation process and the details of the implementation of the creation. The object created is arguably more complex, and the object created is a composite object.