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 Java builder model?

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

Share

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

This article is about what the Java builder model is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Ask questions

Let's say we need to build a house: the process is piling, building walls, and capping. There are all kinds of houses, such as ordinary houses, high-rise buildings, villas, all kinds of houses, although the process is the same, but the requirements are not the same. 3) Please write a program to complete the requirements.

The traditional idea should be in the form of the following class diagram. .

Then the advantage of this way of writing is that it is easy to understand and easy to operate.

The disadvantage is that the design of the program structure is too simple, there is no design of cache layer objects, the expansion and maintenance of the program is not good. In other words, this design scheme enhances the coupling by encapsulating the product (that is, the house) with the process of creating the product (that is, the process of building the house).

Solution: decouple the product and the product construction process = > builder mode.

two。 What is the builder model?

The builder pattern (Builder Pattern >, also known as the generator pattern) is an object-building pattern. It can abstract the construction process of complex objects (abstract categories), so that different implementation methods of this abstract process can construct objects with different representations (attributes).

The builder pattern is to create a complex object step by step, which allows the user to build complex objects only by specifying their type and content. The user does not need to know the internal build details.

There are four major roles in the builder model:

Product (product role): a specific product object.

Builder (Abstract Builder): creates an interface / abstract class specified by various parts of the Product object.

ConcreteBuilder (concrete builder): implement interfaces, build and assemble components.

Director (conductor): build an object that uses the Builder interface. It is mainly used to create a complex object. It mainly has two functions, one is to isolate the production process of the customer and the object, and the other is to be responsible for controlling the production process of the product object.

3. Case code

If the case of building a house on top is solved using the builder model, then the class diagram looks like this. (all four roles are among them), House class is the concrete product (the house entity we want to build), HouseBuilder is the abstract builder, the specific construction process is not realized within it, but is completed by several subclasses below it, these subclasses are the concrete builders (CommonHouse, HighBuilding), and the conductor is HouseDirector, which is responsible for the construction process of the product object (which type of house I want to build in the end). The last Client is our test class.

Package com.szh.builder; public class House {private String basic; private String wall; private String roof; / / setter and getter} package com.szh.builder; / / Abstract builder public abstract class HouseBuilder {protected House house = new House (); / / write the construction process well, abstract method public abstract void buildBasic (); public abstract void buildWall (); public abstract void buildRoof () / / build the house well, return the product (house) to public House buildHouse () {return house;}} package com.szh.builder; public class CommonHouse extends HouseBuilder {@ Override public void buildBasic () {System.out.println ("ordinary house foundation 5m. ");} @ Override public void buildWall () {System.out.println (" the wall of an ordinary house is 10cm. ");} @ Override public void buildRoof () {System.out.println (" ordinary houses add roofs. ");}} package com.szh.builder; public class HighHouse extends HouseBuilder {@ Override public void buildBasic () {System.out.println (" high-rise building foundation 100m. ");} @ Override public void buildWall () {System.out.println (" high-rise building wall 20cm. ");} @ Override public void buildRoof () {System.out.println (" add transparent roofs to tall buildings. ");}} package com.szh.builder; / / Commander, here to specify the production process, return the product public class HouseDirector {HouseBuilder houseBuilder; / / constructor input houseBuilder public HouseDirector (HouseBuilder houseBuilder) {this.houseBuilder = houseBuilder;} / / pass houseBuilder public void setHouseBuilder (HouseBuilder houseBuilder) {this.houseBuilder = houseBuilder through setter } / / how to handle the process of building a house, give it to the commander public House constructHouse () {houseBuilder.buildBasic (); houseBuilder.buildWall (); houseBuilder.buildRoof (); return houseBuilder.buildHouse ();}} package com.szh.builder; public class MainTest {public static void main (String [] args) {/ / build an ordinary house CommonHouse commonHouse = new CommonHouse () / / HouseDirector houseDirector = new HouseDirector (commonHouse) who is going to create the house; / / finish building the house and return the product (ordinary house) houseDirector.constructHouse (); System.out.println ("-"); / / build high-rise building HighHouse highHouse = new HighHouse () / / reset the builder houseDirector.setHouseBuilder (highHouse); / / complete the construction of the house and return the product (tall building) houseDirector.constructHouse ();}}

Builder pattern in 3.JDK

Let's take a look at the StringBuilder class, and its parent class, the related interface implemented by the parent class.

The Appendable interface defines multiple append methods (abstract methods), that is, Appendable is the abstract builder and defines abstract methods.

AbstractStringBuilder implements the Appendable interface method, where AbstractStringBuilder is already the builder, but cannot be instantiated.

StringBuilder acts as a conductor and a concrete builder. The implementation of the construction method is completed by AbstractStringBuilder, while StringBuilder inherits AbstractStringBuilder.

4. Summary of builder model

The client (user program) does not need to know the details of the internal composition of the product, decoupling the product itself from the product creation process, so that the same creation process can create different product objects.

Each specific builder is relatively independent, and has nothing to do with other specific builders, so it is convenient to replace specific builders or add new specific builders. Users can get different product objects by using different specific builders.

The creation process of the product can be controlled more finely. Decomposing the creation steps of complex products into different methods makes the creation process clearer and more convenient to use the program to control the creation process.

Adding a new concrete builder does not need to modify the code of the original class library, and the director class is programmed for the abstract builder class, so the system expansion is convenient and conforms to the "opening and closing principle".

The products created by the builder model generally have more in common, and their components are similar. If there are great differences between products, it is not suitable to use the builder model, so its scope of use is limited to a certain extent.

If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to implement this change, resulting in a large system, so in this case, consider whether to choose the builder mode.

Abstract factory pattern realizes the creation of product family, a product family is such a series of products: product portfolio with different classification dimensions, using abstract factory pattern does not need to care about the construction process, only care about what products are produced by which factories. The builder model requires the product to be built according to the specified blueprint, and its main purpose is to produce a new product by assembling parts.

Thank you for reading! This is the end of the article on "what is the Java Builder Model". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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