Recommended reading:
background
Construction mode (Builder mode)
If there is a need to build a house, the process of building a house is the same: piling, building walls and capping. But houses are all kinds. The last house built may be a tall building or villa.
According to the direct thinking, without the idea of design pattern, we may:
- Write an abstract class of CommonHouse, which specifies three methods: piling, wall building and capping;
- Let different houses inherit this class;
- Finally, call the separate method when calling.
There is no problem with the idea of inheriting abstract classes and subclasses. The problem arises from these classes themselves.
Disadvantages:
Too simple, the product (house) and the creation product (house construction process) are encapsulated together, and the coupling is enhanced. (it can be understood that in the object-oriented idea, although the house is a class and has its own methods, the house should not have its own methods to build)
resolvent:
Decoupling - > builder mode.
1, Builder mode
Builder Pattern, also known as Builder Pattern, is an object construction pattern (creation type), which can abstract the construction process of complex objects (abstract classes), so that different implementation methods of this abstract process can construct objects with different representations (attributes).
The construction mode allows users to build complex objects only by specifying the type and content of them, and users do not need to know the specific internal construction details.
The four roles of the builder model:
- Product: a specific product object;
- Builder (Abstract builder): create the interface or abstract class specified by the Product object;
- Concrete Builder: realize the interface, build and assemble various components;
- Director Commander: build an object using the Builder interface, which is mainly used to create a complex object. It has two functions: one is to isolate the production process of customers and objects, and the other is to control the production process of product objects.
The relationship between them is explained by class diagram:
- Aggregating a Builder in director actually uses its implementation class ConcreteBuilder;
- There can be many concrete builders, which are called different house builders.
Because getRusult is the same, there is no interface for the time being, and the Builder is implemented with abstract classes. The code is as follows:
/* Product, corresponding to product */ public class House { private String base; private String wall; private String roof; //Corresponding getset method }
/* Abstract Builder, corresponding to Builder */ public abstract class HouseBuilder { protected House house = new House(); //Write all methods of the process, but do not restrict the specific implementation public abstract void buildBasic(); public abstract void buildWalls(); public abstract void buildRoof(); //Construction method, return construction result public House buildHouse(){ return house; } }
/* Ordinary house manufacturing process, inheriting abstract classes You can see that the manufacturing process is here, and the House class has the properties of the House. They are separated It should be the operation of House, which is omitted here */ public class CommonHouse extends HouseBuilder{ @Override public void buildBasic() { System.out.println("Ordinary house: Building Foundation..."); } @Override public void buildWalls() { System.out.println("Ordinary house: building walls..."); } @Override public void buildRoof() { System.out.println("Ordinary house: build a roof..."); } }
/* Another implementation class, which should have been an operation on House, is omitted here */ public class HighHouse extends HouseBuilder { @Override public void buildBasic() { System.out.println("Tall building: Building Foundation..."); } @Override public void buildWalls() { System.out.println("High rise: building walls..."); } @Override public void buildRoof() { System.out.println("Tall building: roof..."); } }
/* Director,Aggregator HouseBuilder At the same time, determine the production process, and finally call the builder's buildHouse method to return. */ public class Director { HouseBuilder houseBuilder = null; //Aggregation by constructor public Director(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //Aggregate through setter method public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //Direct the specific construction process, and the order is not determined by the Builder public House constructHouse(){ houseBuilder.buildBasic(); houseBuilder.buildWalls(); houseBuilder.buildRoof(); return houseBuilder.buildHouse(); } }
/* client */ public class Client { public static void main(String[] args) { //new house CommonHouse commonHouse = new CommonHouse(); //new Commander Director director = new Director(commonHouse); //Complete the building House house = director.constructHouse(); //Reset builder HighHouse highHouse = new HighHouse(); director.setHouseBuilder(highHouse); House house1 = director.constructHouse(); } }
The above code can be summarized as follows: first, the House should be classified as a House, and the attribute is enough. The construction belongs to construction, which is equivalent to the separation of construction workers and the two objects of the House. Then the Director is equivalent to the contractor, and it is necessary to command different Builder groups for different houses.
2, Application of builder mode in JDK
java.lang.StringBuilder class, that is, the commonly used variable string class, uses the builder mode. Take the common method append method as an example to see the source code.
2.1 Appendable interface
Multiple append methods (abstract methods) are defined, that is, Appendable is our abstract Builder.
2.2 specific implementation class AbstractStringBuilder
Although it is also an abstract class, it has also implemented the method of the Append able interface, so it is actually equivalent to the concrete builder ConcreteBuilder;
2.3 StringBuilder, inheriting AbstractStringBuilder
However, he rewrites the append method only by calling the parent method, so it should be said that StringBuilder acts as both Director and ConcreteBuilder
3, Precautions
- The client does not need to know the details of the internal composition of the product, and decouples the product itself from the product creation process, so that the same creation process can create different products;
- Each specific builder is relatively independent and has nothing to do with other builders. Therefore, it is convenient to replace specific builders or add new specific builders, and users can get different products by using different specific builders;
- The creation process can be more finely controlled;
- Adding a new concrete builder does not need to modify the original code, and the commander programs for the abstract builder;
- The products created by the builder mode must have more in common and similar components. If there are great differences between products, it is not suitable to use the builder mode, so its scope of application is limited.
Builder mode VS abstract factory mode:
Abstract factory mode realizes the creation of product family. A product family: product combinations with different classification dimensions. Adopting abstract factory mode does not need to care about the construction process, but only about what products are produced by what factory.
***
Learning technology must formulate a clear learning route, so as to learn efficiently. There is no need to do ineffective work, which is a waste of time and no efficiency. You may as well follow my route to learn.
Final interview sharing
You may as well brush more questions directly on Niuke and Likou. At the same time, I also took some interview questions to share with you, which is also obtained from some big guys. You may as well brush more questions and make a wave for golden nine silver ten!
[external chain picture transferring... (img-j12xnuBF-1623750073111)]
Final interview sharing
You may as well brush more questions directly on Niuke and Likou. At the same time, I also took some interview questions to share with you, which is also obtained from some big guys. You may as well brush more questions and make a wave for golden nine silver ten!
[external chain picture transferring... (img-uAOz9csj-1623750073112)]
[external chain picture transferring... (img-3ggegjyo-1623750073113)]
Finally, if you need a complete pdf version, you can praise this article Click here for free