There are 23 modes in the design pattern. This is just for one purpose: decoupling + decoupling + decoupling ... ( cohesion poly-low coupling satisfies the opening and closing principle)
Introduced:
The builder mode is a step-by-step creation of a complex object that allows the user to specify only complex objects.
Separating the construction of a complex object from its representation allows the same build process to create different representations.
Pattern structure:
First, let's look at the builder code:
Public InterfaceBuilder {//creating a part A such as creating a car wheel voidBuildparta (); //creating a part B such as creating a car steering wheel voidBUILDPARTB (); //creating a part C such as creating a car engine voidBUILDPARTC (); //returns the final assembly of the finished product (back to the last assembled car)//the assembly process for the finished product is not carried out here, but instead is transferred to the following Director class. //thus realizing the understanding of the coupling process and componentsProduct GetResult ();}
Next, let's take a look at the command director.
Public class Director { private builder Builder; public Director (Builder builder) { this. Builder = Builder; } // parts Parta partB PARTC final composition complex object // This is the process of assembling the wheel steering wheel and the engine into a car. public void construct () {Builder.buildparta (); BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC (); }}
And then the concrete builder.
Public classConcreteBuilderImplementsBuilder {Part parta, PartB, PARTC; Public voidBuildparta () {//here's how to build Parta code specifically}; Public voidBUILDPARTB () {//here's how to build PARTB code specifically}; Public voidBUILDPARTC () {//here's how to build PARTB code specifically}; PublicProduct GetResult () {//return to final assembly of finished product results}; }
In this way we only need to do this in the client code:
Newnew= Builder.getresult ();
Builder is relatively simple, if you have complex objects to create when you can apply the sub-mode, you can also use the Director to control the composition of complex objects, such as mail (including attachments, topics, content ...) )
Builder of Design Patterns