“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The preface
The design pattern we’re going to look at today is called the Builder pattern, and the Builder pattern helps us create a complex object by separating the construction of a complex object from its results, so that the same construction process can create different results. Unlike the factory model in the previous article, the factory model does not focus on process, but only on results. For those of you who don’t know factory patterns, check out this article: Playing with Design Patterns (Simple Factory Patterns).
What is the Builder pattern
The Builder pattern uses scenarios that mostly occur when there is a lot of repetition in the process, but the end result is different. Take a simple example, there is an automatic cooking machine, cooking logic is written in the machine program, all the steps may include:
- Put some oil in, and then heat it up
- And then we add the dish
- And then keep flicking
- And then we add the seasoning
- Then on the plate
We found that, whether we want Fried cabbage, or stew, as above steps, but different dishes, need to master different oil, need time is different, the spice of need is different also, are we going to cabbage and stew, respectively, respectively, to define a such a long process, the builder pattern can let two is generated by a set of logic programs.
Builder pattern instance
class FriedDish { name: string; MeatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType = meatType : string; vegetableType? : string; private breadNum? : number; Constructor (name: string) {// If we do not use the constructor pattern, the constructor of the product class will pass all arguments this.name = name; } // Set breadNum (num){this.breadNum = num; } setBreadNum(num: number) { this.breadNum = num; Abstract class FriedDishBuilder {abstract buildBread(breadNum: number): void; abstract buildMeat(meatType: string): void; abstract buildVegetable(vegetableTYpe: string): void; abstract createHamburg(): FriedDish; } class FriedDish beef extends FriedDishBuilder {// Private FriedDish extends FriedDish builder FriedDish = new FriedDish(' beef '); BuildBread (breadNum: number): void {console.log(' ${breadNum} slices'); this.FriedDish.setBreadNum(breadNum); } buildMeat(meatType: string): void {console.log(' ${meatType} '); this.FriedDish.meatType = meatType; } buildVegetable(vegetableType: String): void {console.log(' Beef needs ${vegetableType} '); this.FriedDish.vegetableType = vegetableType; } createHamburg(): FriedDish { return this.FriedDish; }} class FriedDish extends FriedDishBuilder {private FriedDish: FriedDish = new FriedDish(' pork '); BuildBread (breadNum: number): void {console.log(' ${breadNum} slices'); this.FriedDish.setBreadNum(breadNum); } buildMeat(meatType: string): void {console.log(' ${meatType} '); this.FriedDish.meatType = meatType; } buildVegetable(vegetableType: String): void {console.log(' Make pork need ${vegetableType} '); this.FriedDish.vegetableType = vegetableType; } createHamburg(): FriedDish { return this.FriedDish; If you want to change the process, just add a new director or add a new construct class HamburgDirector {// order 1, Static Construct1 (Builder: FriedDishBuilder, breadNum: number, meatType: String, vegetableType: String) FriedDish { builder.buildBread(breadNum); builder.buildMeat(meatType); builder.buildVegetable(vegetableType); return builder.createHamburg(); Static construct2(Builder: FriedDishBuilder, breadNum: number, meatType: string): FriedDish { builder.buildMeat(meatType); builder.buildBread(breadNum); return builder.createHamburg(); } } const FriedDishBeef = new FriedDishBeef(); const FriedDishPork = new FriedDishPork(); HamburgDirector.construct1(FriedDishBeef, 2, 'beef', 'carrot'); HamburgDirector.construct2(FriedDishPork, 3, 'pork');Copy the code
conclusion
As we have seen from the above examples, when a class has the same steps, we can change the parameters of the steps to get the results we want, and when we need to add new ones, we have a standard specification and definition. Very easy to manage.