介绍
建造者模式又称生成器模式,分步构建一个复杂对象,并允许按步骤构造。同样的构建过程可以采用不同的表示,将一个复杂对象的构建层与其表示层分离。
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
| class ProductBuilder { constructor(param) { this.param = param } buildPart1() { this.part1 = 'part1' } buildPart2() { this.part2 = 'part2' } }
class Director { constructor(param) { const _product = new ProductBuilder(param) _product.buildPart1() _product.buildPart2() return _product } }
const product = new Director('param')
console.log(product);
|