JavaScript Design Patterns sample five - builder mode

Builders mode (Builder Pattern)

Definition: using a plurality of simple objects step by step to construct a complex object. 
 Objective: The complex build it represents a phase separation process such that the same construct can create different representations. 
 Scene: Some basic components will not change, and time combinations change frequently.
// declare a product class 
class Product { 
    constructor () { 

    } 
} 

// declare a builder class 
class BuilderProduct { 
    constructor () { 
        // build the product name 
        this.nameBuilder = (name) => { 
            this.name = name | | null 
        } 
        // build product version 
        this.versionBuilder = (version) => { 
            this.version version || = null 
        } 
        // build production date 
        this.createTimeBuilder = (createTime) => { 
            this.createTime = createTime || null 
        } 
        // build the product composition 
        this.getProduct = () => { 
            the let new new product product = () 
            IF (this.name) {
                product.name = this.name
            }

            if (this.version) {
                product.version = this.version
            }

            if (this.createTime) {
                product.createTime = this.createTime
            }

            return product
        }
    }

}

// 声明一个需求类
class Demand {
    constructor (demands) {
        let builderProduct = new BuilderProduct()
        builderProduct.nameBuilder(demands.name)
        builderProduct.versionBuilder(demands.version)
        builderProduct.createTimeBuilder(demands.createTime)
        return builderProduct.getProduct()
    }
}

let demand = new Demand({
    name: 'SKILL.NULL'
})

Git Address: https://github.com/skillnull/Design-Mode-Example

Guess you like

Origin www.cnblogs.com/Man-Dream-Necessary/p/12362889.html