js design pattern builder pattern

Keywords: Programming

Builder mode: separate the construction layer of a complex object from its representation layer, and the same construction process can adopt different representations. It is mainly used to build a responsible object step by step, in which "step by step" is a stable algorithm, while the parts of complex objects often change. Let's use a code to explain:

// Create a candidate object
// This complex candidate can be divided into three objects:
// 1. Human object
// 2. Name object
// 3. Working object

//Human beings
var Human = function (param) {
    if (!this instanceof Human) {
        return new Human(param);
    } else {
        this.skill = param && param.skill || 'secrecy';
        this.hobby = param && param.hobby || 'secrecy';
    }
}
Human.prototype.getSkill = function () {
    return this.skill;
}
Human.prototype.getHobby = function () {
    return this.hobby;
}

//Name category
var Named = function (name) {
    if (!this instanceof Named) {
        return new Named(name);
    } else {
        this.wholeName = name;
        var index = name.indexOf(' ');
        if (index > -1) {
            this.firstName = name.slice(0, index);
            this.lastName = name.slice(index);
        }
    }
}

//Position category
var Work = function (work) {
    if (!this instanceof Work) {
        return new Work(work);
    } else {
        switch ('work') {
            case 'code':
                this.work = 'engineer';
                this.workDescript = 'Addicted to programming every day';
                break;
            case 'UI':
            case 'UE':
                this.work = 'designer';
                this.workDescript = 'Design is more like an art';
                break;
            case 'teach':
                this.work = 'Teacher';
                this.workDescript = 'Sharing is also a pleasure';
                break;
            default:
                this.work = work;
                this.workDescript = 'Sorry, we don't know the description of your chosen occupation';
        }
    }
}
Work.prototype.changeWork = function(work){
    this.work = work;
}
Work.prototype.changDescript = function(setence){
    this.workDescript = setence;
}

//Candidate builder

var Person = function(param, name, work){
    var _person = new Human(param);
    _person.name = new Named(name);
    _person.work = new Work(work);
    return _person;
}
var person = Person({
    skill: 'code',
    hobby: 'english'
},'jack li', 'code');
In the above example, we split a complex candidate's object into three objects: human object, name object and work object. In the actual process, the specific contents of these three objects may change according to different needs, but a candidate is composed of these three objects. This algorithm is relatively stable, so when the needs change, we only You need to change the corresponding object.
The advantage of the builder mode is that "processing technology" is exposed, which makes the builder mode more flexible, and the builder mode decouples the assembly process and specific parts, so that we do not have to care about how each part is assembled.

Of course, this pattern increases the complexity of the structure invisibly, so if the object granularity is very small, or the reuse rate between modules is very low and the change is not big, we'd better create the overall object.


Posted by avillanu on Fri, 01 May 2020 13:28:55 -0700