1. Builder mode

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. Creator mode hides the creation process of complex objects. It abstracts the creation process of complex objects and dynamically creates objects with compound attributes through subclass inheritance or overloading.Copy the code

Let’s start with an example

1. Demand for housing construction project
(1) need to build a house: this process for piling, wall, top (2) the house has a variety of, such as ordinary room, high-rise, villa, all kinds of house process although the same, but not the same requirements. (3) please write procedures to complete the requirements.Copy the code
2. The traditional way
Public Abstract class AbstractHouse {public abstract void buildBasic(); Public void buildWalls(); Public abstract void roofed(); Public void build() {buildBasic(); buildWalls(); roofed(); }} public class AbstractHouse extends AbstractHouse {@override public void buildBasic() { System.out.println(" ordinary house foundation "); } @override public void buildWalls() {system.out.println (" buildWalls "); } @override public void roofed() {system.out.println (" roofed "); }} public class abstrbuilding extends AbstractHouse {@override public void buildBasic() { System.out.println(" high-rise foundation "); } @override public void buildWalls() {system.out.println (" buildWalls "); } @override public void roofed() {system.out.println (" roofed "); }} public class Test {public static void main(String[] args) {AbstractHouse commonHouse = new commonHouse (); commonHouse.build(); AbstractHouse highBuilding = new HighBuilding(); highBuilding.build(); }}Copy the code
2. Traditional problem analysis

(1) Advantages are relatively easy to understand, simple and easy to operate.

(2) The design of the program structure, too simple, no design of the cache layer object, the expansion and maintenance of the program is not good. In other words, this design approach encapsulates the product (i.e., the house) and the process of creating the product (i.e., the process of building the house), enhancing coupling.

(3) Solution: Decouple the product from the product construction process => Builder pattern.

Basic introduction to builder mode

Builder model

Builder Pattern: Separates the construction of a complex object from its representation so that the same construction process can create different representations. The Builder pattern is an object creation pattern.

The Builder pattern creates a complex object step by step, allowing users to build complex objects simply by specifying their type and content, without needing to know the specific build details inside.

role

Builder: It specifies an abstract interface for creating the parts of a Product object. In this interface, two classes of methods are generally declared. One is buildPartX(), which is used to create the parts of a complex object. Another class of methods is getResult(), which are used to return complex objects. A Builder can be an abstract class or an interface.

ConcreteBuilder: This implements the Builder interface, implements concrete construction and assembly methods for parts, defines and specifies the complex objects it creates, and can also provide a method to return the created complex product objects.

Product (Product role) : It is a complex object that is built, consisting of multiple components, and the concrete builder creates an internal representation of the Product and defines its assembly process.

Director: Director is also called Director class, which is responsible for arranging the construction order of complex objects. There is an association between Director and abstract builder. The component construction and assembly method of builder object can be called in construct() to complete the construction of complex objects. The client typically only needs to interact with the conductor, determine the type of the concrete builder on the client side, instantiate the concrete builder object (also via configuration files and reflection mechanisms), and pass that object into the conductor class via the constructor or Setter method of the conductor class.

Complex objects are mentioned in the definition of the Builder pattern, so what are complex objects? In simple terms, complex objects refer to those objects that contain multiple member attributes, which are also called parts or parts. For example, a car includes steering wheel, engine, tire and other parts, and an email includes sender, recipient, subject, content, attachment and other parts

The sample

1. Product role House

public class House { private String baise; private String wall; private String roofed; // omit getter, setter, toString}Copy the code

2. HouseBuilder

Public abstract class HouseBuilder {// Return a built house in the abstract builder. // Also define abstract methods in the abstract builder. Protected House House = new House(); Public abstract void buildBasic(); public abstract void buildWalls(); public abstract void roofed(); Public House buildHouse() {return House; }}Copy the code

3. Specific builder HighBuilding, CommonHouse

Public class HighBuilding extends HouseBuilder {@override public void buildBasic() {system.out.println (" HighBuilding extends HouseBuilder {@override public void buildBasic() {system.out.println ( "); } @override public void buildWalls() {system.out.println (" buildWalls 20cm "); } @override public void roofed() {system.out.println (" roofed "); }} public class HouseBuilder extends HouseBuilder {@override public void buildBasic() {system.out.println () "); } @override public void buildWalls() {system.out.println (" buildWalls "); } @override public void roofed() {system.out.println (" roofed "); }}Copy the code

4. HouseDirector directs the construction process

Public class HouseDirector {HouseBuilder HouseBuilder = null; HouseBuilder public HouseDirector(houseBuilder) {this.houseBuilder = houseBuilder; } public void setHouseBuilder(houseBuilder){this.houseBuilder = houseBuilder; Public House constructHouse() {houseBuilder.buildbasic (); public House constructHouse() {houseBuilder.buildbasic (); houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.buildHouse(); }}Copy the code

5. Client test

Public class Test {public static void main(String[] args) {// CommonHouse CommonHouse = new CommonHouse(); HouseDirector HouseDirector = new HouseDirector(commonHouse); / / finish building a House, return the product (normal) House House House. = houseDirector constructHouse (); // system.out. println(" output process "); System.out.println("--------------------------"); // HighBuilding HighBuilding = new HighBuilding(); / / reset builder houseDirector setHouseBuilder (highBuilding); / / finish building a house, return the product (tall) houseDirector. ConstructHouse (); }}Copy the code

You can create specific builder objects through reflection and configuration files

public class Test { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { HouseDirector houseDirector = new HouseDirector(); / / read from the database or in configuration files specific builder Class name Class c = Class. The class.forname (" com.designpattern.Com monHouse "); CommonHouse commonHouse = (CommonHouse) c.newInstance(); House commonHouser = houseDirector.setHouseBuilder(commonHouse); System.out.println(commonHouse.toString()); }}Copy the code

Notes and details of builder mode

Advantages:

1. Using the Builder pattern allows clients to avoid having to know the details of the product's internal composition. 2. The concrete builder classes are independent of each other, which helps to scale the system. 3. The specific builders are independent of each other, so they can gradually refine the construction process without any impact on other modules. 4. More fine-grained control over the product creation process. Breaking down the creation steps of complex products into different methods makes the creation process clearer and easier to control programmatically. 5, add new concrete builder without modifying the original class library code, commander class for abstract builder class programming, system expansion is convenient, in line with the "open and closed principle".Copy the code

Disadvantages:

1. The products created by the Builder mode generally have more in common and their components are similar. If there is a large difference between the products, the builder mode is not suitable to be used, so its application scope is limited to some extent. 2. If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to implement the changes, resulting in a large system. In this case, it is important to consider whether to choose the builder pattern.Copy the code

Abstract Factory pattern vs. Builder pattern

In contrast to the Abstract Factory pattern, the Builder pattern returns an assembled complete product, while the Abstract Factory pattern returns a series of related products that reside in different product hierarchy structures and constitute a product family. In the abstract factory pattern, the client class instance chemical plant, and then call the factory method to obtain the required product object, in the builder pattern, the client can not directly call the builders of relevant methods, but by commanders class to guide how to generate object, including the object of the assembly process and construction steps, step by step, it focuses on the construction of a complex object, Returns a complete object. If you think of the abstract factory pattern as an auto parts production factory that produces the products of a product family, then the Builder pattern is an auto assembly factory that returns a complete car by assembling the partsCopy the code

Builder mode in JDK application and source code analysis

To fillCopy the code