Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the builder pattern of the Java design pattern

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of what the builder pattern of Java design pattern is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the builder pattern of Java design pattern. Let's take a look at it.

1. What is the builder model?

We know that in the process of software development, it is sometimes necessary to create a very complex object, which is usually composed of multiple sub-components according to certain steps.

For example, when we assemble a computer, we need CPU, motherboard, memory, hard disk, graphics card, chassis, monitor, keyboard, mouse and other components. For example, if the school needs to purchase 100 computers, it is impossible for the school to buy the parts and assemble them by themselves. it must tell the purchasers what kind of configuration they need, and then the purchasers will go to the computer company to buy them, and the company will help you assemble them and give them to the purchasers. Finally, give it to the school.

The above example shows that each part can be flexibly selected (including the level of each configuration of the computer), but the creation steps are more or less the same. However, the creation of such products cannot be described by the factory pattern described earlier, and only the builder pattern can well describe the creation of such products.

2. The definition of builder model.

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 decomposes a complex object into several simple objects, and then builds them step by step. It will separate the change from the unchanged phase, that is, the components of the product are unchanged, but each part can be flexibly selected.

To sum up: separate the construction of a complex object from its representation, so that the same construction process can create different representations

Types: creating class schema

3. Advantages and disadvantages of builder model.

Advantages:

Good encapsulation, separation of build and presentation

The expansibility is good, and the specific builders are independent of each other, which is conducive to the decoupling of the system.

The client does not need to know the details of the internal composition of the product, and the builder can refine the creation process step by step without having any impact on other modules, so it is easy to control the risk of details.

Disadvantages:

The components of the product must be the same, which limits its scope of use

If the internal change of the product is complex, if the internal change of the product occurs, the builder should also modify it synchronously, and the cost of maintenance is high in the later stage.

Note:

The focus of the builder model is different from that of the factory model: the builder model focuses on the assembly process of parts, while the factory method model pays more attention to the creation process of parts, but the two can be used together.

4. The structure of the builder model.

The main roles of the builder model are as follows:

Product role (Product): a complex object with multiple components, each of which is created by a specific builder

Abstract builder (Builder): implements the interface of Builder to construct and assemble parts of the product, defines and defines the representations it creates, and provides an interface to retrieve the product

Concrete builder (Concrete Builder): construct an object using the Builder interface to guide the construction process

Director: it calls the component construction and assembly methods in the builder object to complete the creation of complex objects, and does not involve specific product information in the commander.

Structure diagram (reference from the Internet):

5. Builder mode code demonstration

For example, now you need to build a car:

Product category:

/ * * Product category * / public class Car {/ / the construction process of the car private String wheel; / / Wheel private String skeleton; / / frame private String engine; / / engine public String getWheel () {return wheel;} public void setWheel (String wheel) {this.wheel = wheel;} public String getSkeleton () {return skeleton } public void setSkeleton (String skeleton) {this.skeleton = skeleton;} public String getEngine () {return engine;} public void setEngine (String engine) {this.engine = engine;}}

Instructor class:

/ * instructor * / / Automotive Director public class CarDirector {public Car constructCar (ICarBuilder builder) {builder.buildwheel (); builder.buildSkeleton (); builder.buildEngine (); return builder.buildCar ();}}

Specific builders:

/ * * specific builder: produce specific things * / public class ConcreteBuilder implements ICarBuilder {Car car; public ConcreteBuilder () {car = new Car ();} @ Override public void buildwheel () {car.setWheel ("wheels");} @ Override public void buildSkeleton () {car.setSkeleton ("body structure") } @ Override public void buildEngine () {car.setEngine (engine);} @ Override public Car buildCar () {return this.car;}}

Abstract builder (interface):

/ * Abstract Builder * / public interface ICarBuilder {public void buildwheel (); / / build wheel public void buildSkeleton (); / / build skeleton public void buildEngine (); / / build engine Car buildCar ();}

Test class:

Public class Test {public static void main (String [] args) {/ / create a new director (to buy a car for you) CarDirector director = new CarDirector (); Car car = director.constructCar (new ConcreteBuilder ()); System.out.println (car.getWheel ()); System.out.println (car.getEngine ()); System.out.println (car.getSkeleton ());}}

After reading the code demonstration, and then combined with the first part of the article, you can understand it!

6. The application scenario of the builder model

The only difference between the builder pattern and the factory pattern is the creation of complex objects. That is, if you create a simple object, you usually use the factory pattern, while if you create a complex object, you can consider using the builder pattern

Main application scenarios:

The same method, different execution order, produce different results

Multiple assemblies or parts can be assembled into one object, but the results are different.

The product class is very complex, or the different calling order in the product class has different effects.

Initializing an object is very complex, there are many parameters, and many parameters have default values

7. The difference between the builder model and the factory model

The builder pattern pays more attention to the calling order of methods, and the factory pattern focuses on creating objects.

The strength of creating objects is different, the builder pattern creates complex objects, which is made up of various complex components, and the objects created by the factory pattern are all the same.

The focus is different. The factory pattern only needs to create the object, while the builder pattern needs not only to create the object, but also to know what parts the object is made of.

The builder pattern varies according to the order of the construction process, and the composition of the final object components is also different.

This is the end of the article on "what is the Builder pattern of Java Design pattern?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the builder pattern of Java design pattern". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report