Writing in the front

  • Take notes on learning design patterns
  • Improve flexibility in the use of design patterns

Learning to address

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

Refer to the article

http://c.biancheng.net/view/1…

Project source https://gitee.com/zhuang-kang/DesignPattern

8. Builder mode

8.1 Definition and characteristics of the Builder pattern

Definition of the Builder pattern: A design pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations is called the Builder pattern. It is the decomposition of a complex object into many simple objects, and then build step by step. It separates change from immutability, that is, the components of a product are constant, but each part can be flexibly selected.

The main advantages of this pattern are as follows:

  1. Good encapsulation, separation of construction and presentation.
  2. Good expansibility, each specific builder is independent of each other, which is conducive to the decoupling of the system.
  3. The client does not have to know the details of the internal composition of the product, and the builder can gradually refine the creation process without having any impact on other modules, thus controlling the risk of details.

Its disadvantages are as follows:

  1. The components of a product have to be the same, which limits their range of use.
  2. If the internal change of the product is complex, if the internal change of the product occurs, the builder will also have to modify it simultaneously, and the maintenance cost will be large in the later stage.

8.2 Structure and implementation of the builder pattern

8.2.1 Structure of Builder Mode

  1. Product role (Product) : It is a complex object with multiple component parts that are created by a concrete builder.
  2. Abstract Builder: This is an interface that contains an abstract method to create the various child parts of a product, usually including a method getResult() that returns a complex product.
  3. Concrete Builder: It is a Concrete method to realize the Builder interface and complete the creation of various parts of complex products.
  4. Director: It calls the component construction and assembly methods in the builder object to complete the creation of complex objects, and does not involve product-specific information in the Director.

8.2.2 Code implementation

House Product Roles

package com.zhuang.builder; /** * @ClassName House * @Description * @Date 2021/3/20 11:49 * @Created by Dell */ public class House {private String ground; private String wall; private String roofed; public House() { } public House(String ground, String wall, String roofed) { this.ground = ground; this.wall = wall; this.roofed = roofed; } public String getGround() { return ground; } public void setGround(String ground) { this.ground = ground; } public String getWall() { return wall; } public void setWall(String wall) { this.wall = wall; } public String getRoofed() { return roofed; } public void setRoofed(String roofed) { this.roofed = roofed; } @Override public String toString() { return "House{" + "ground='" + ground + '\'' + ", wall='" + wall + '\'' + ", roofed='" + roofed + '\'' + '}'; }}

HouseBuilder Abstract Builder

package com.zhuang.builder; /** * @ClassName HouseBuilder * @Description * @Date 2021/3/20 11:49 * @Created by Dell */ public abstract class HouseBuilder {// create protected House = new House(); Public void buildGround(); public abstract void buildWall(); public abstract void buildRoofed(); Public House getHouse() {return House; }}

Highhouse builder

package com.zhuang.builder; /** * @ClassName HighHouse * @Description * @Date 2021/3/20 11:51 * @Created by Dell */ public class HighHouse Extends HouseBuilder {@Override public void buildGround() {house.setGround("100 "); Println (" tall building: foundation "); } @Override public void buildWall() {house.setWall("50 "); Println (" tall building: 50 meters of walls "); } @Override public void buildroOfEd () {house. builetroOfEd (" skylight "); Println (" villa: cover skylight "); }}

Villahouse builder

package com.zhuang.builder; /** * @ClassName VillaHouse * @Description * @Date 2021/3/20 11:51 * @Created by Dell */ public class VillaHouse Extends HouseBuilder {@Override public void buildGround() {house.setGround("200 "); Println (" villa: foundation "); } @Override public void buildWall() {house.setWall("10 "); Println (" Villa: 10 meters by laying walls "); } @Override public void buildroOfEd () {house. builetroOfEd (" ceiling "); Println (" villa: ceiling "); }}

HouseDirector commanders

package com.zhuang.builder; /** * @ClassName HouseDirector * @Description Project Manager * @Date 2021/3/20 11:50 * @Created by Dell */ public class HouseDirector { private HouseBuilder houseBuilder; public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } public House build() { houseBuilder.buildGround(); houseBuilder.buildWall(); houseBuilder.buildRoofed(); return houseBuilder.getHouse(); }}

Client

package com.zhuang.builder; /** * @ClassName Client * @Description * @Date 2021/3/20 11:51 * @Created by Dell */ public class Client { public static void main(String[] args) { House house1 = new HouseDirector(new VillaHouse()).build(); System.out.println(house1); System.out.println("============================================"); House house2 = new HouseDirector(new HighHouse()).build(); System.out.println(house2); }}

8.3 Application scenarios for the Builder pattern

  • The same method, executed in different order, produces different results.
  • Multiple parts, or parts, can be assembled into an object, but produce different results.
  • The product class is very complex, or different order of invocation in the product class has different effects.
  • Initializing an object is particularly complex with many parameters, many of which have default values.

9. Creator mode comparison

9.1 Factory Method Mode vs. Builder Mode

The factory method pattern focuses on how the whole object is created; The Builder pattern focuses on the process of building parts, with the intention of creating a complex object through step-by-step precise construction.

Let’s take a simple example to illustrate the difference between the two. If we want to make a Superman, if we use the factory method mode, we will directly produce a Superman with infinite strength, who can fly and wear his underwear. If you use the builder mode, you need to assemble the hands, head, feet, torso, and so on, and then put on the underwear, and you have a Superman.

9.2 Abstract Factory vs. Builder

Abstract factory pattern realizes the creation of a product family. A product family is a series of products: a product portfolio with different classification dimensions. Using abstract factory pattern, we do not need to care about the construction process, but only care about which products are produced by which factory.

Builder mode requires the product to be built according to a specified blueprint, and its main purpose is to assemble parts to produce a new product.

If you think of the abstract factory model as an auto parts factory that produces the products of a product family, then the builder model is an auto assembly factory that returns a complete car by assembling the parts.

  • The builder pattern focuses more on the order in which methods are called, and the factory pattern focuses on creating objects.
  • The intensity of object creation varies. The Builder pattern creates complex objects made up of complex parts. The Factory pattern creates the same object
  • The focus is different, whereas the Factory pattern only needs to create the object, the Builder pattern not only needs to create the object, but also needs to know what parts the object is made of.
  • The builder pattern varies depending on the order of the build process, and the final composition of the object parts varies.

Write in the last

  • If my article is useful to you, please click 👍, thank you!
  • Let me know in the comments section if you have any questions! 💪