Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The builder pattern is also called the generator pattern

Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Copy the code

The construction of a complex object is separated from its representation so that the same construction process can create useless representations.

Generic class diagram for builder pattern:

As you can see from the structure above, there are four main roles:

  • Abstract Builder: The specification of the building of a product, containing an interface to abstract methods for creating the various child parts of the product, and usually a method for returning a complex product, getResult().
  • Director: “Responsible for ordering existing modules and then telling Builder to start building”
  • Concrete Builder: Implements all the methods defined by the abstract class Builder and returns a component object
  • Product: It is a complex object with multiple components that are created by a concrete builder.

Implement a demo using the Builder to analyze:

class App { private name: string | undefined; private version: string | undefined; } class AppBuilder {protected app = new app (); public abstract requirementDoc(): void; public abstract codeProcess(): void; public abstract testProcess(): void; public abstract upTimeProcess(): void; public buidlApp() { return this.app; } // Class WebApp extends AppBuilder {public requirementDoc(): void {console.log(' Web development: Requirements stage '); } public codeProcess(): void {console.log(' Web development: Coding phase '); } public testProcess(): void {console.log(' Web development: Test phase '); } public upTimeProcess(): void {console.log(' Web development: Live '); }} Class PhoneApp extends AppBuilder {public requirementDoc(): void {console.log(' Mobile App development: Requirements stage '); } public codeProcess(): void {console.log(' mobile app development: Coding phase '); } public testProcess(): void {console.log(' mobile app development: test '); } public upTimeProcess(): void {console.log(' mobile app development: test '); }} class AppDirector {public appBuilder: appBuilder; // Constructor (appBuilder: appBuilder) {this.appBuilder = appBuilder; } public setAppBuilder(appBuilder: AppBuilder) { this.appBuilder = appBuilder; } public createApp(): App { this.appBuilder.requirementDoc(); this.appBuilder.codeProcess(); this.appBuilder.testProcess(); this.appBuilder.upTimeProcess(); return this.appBuilder.buidlApp(); }} / / -- -- -- test -- -- -- (() = > {const webApp = new webApp (); const appdirector = new AppDirector(webApp); appdirector.createApp(); const phoneApp = new PhoneApp(); const appdirector2 = new AppDirector(phoneApp); appdirector2.createApp(); }) (); // -- Output result -- Web development: Requirement Stage Web development: Coding stage Web development: Testing stage Web development: Launch stage Mobile APP development: Requirement Stage Mobile app development: Coding stage Mobile app development: Testing stage Mobile app development: Testing stageCopy the code

The director of AppDirector and the decoupling of the specific internal composition details of each product, AppBuilder and PhoneBuilder are independent of each other, which is very beneficial to the expansion of the system.

However, if the process of building each product is complex, there will be many build classes, and the products must have something in common and the entire build process must be the same, leading to scope limitations

Note that the Builder mode focuses more on product type and build order, so it has more usage scenarios as follows:

  • The same method, different execution sequence, produces different event results when using the Builder pattern
  • Multiple procedures, all of which can be assembled into an object but produce different results, use the Builder pattern
  • The Builder pattern is used when the product class is complex, or when the invocation sequence in the product class is different and results in different performance
  • The Builder pattern is used when other objects in the system are used during object creation and are not readily available in the creation of product objects