-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuilder-Pattern.js
59 lines (50 loc) · 1.36 KB
/
Builder-Pattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 建造者模式(Builder Pattern)
// 声明一个产品类
class Product {
constructor () {
}
}
// 声明一个建造者类
class BuilderProduct {
constructor () {
// 建造产品名称
this.nameBuilder = (name) => {
this.name = name || null
}
// 建造产品版本
this.versionBuilder = (version) => {
this.version = version || null
}
// 建造产品生产日期
this.createTimeBuilder = (createTime) => {
this.createTime = createTime || null
}
// 组合建造产品
this.getProduct = () => {
let product = new 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'
})