What is Java Builder Pattern
1. Ask a question
Suppose we need to build a house: the process is piling, building walls, and capping. There are various types of houses, such as ordinary houses, high-rise buildings, and villas. Although the process is the same for all kinds of houses, 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. .
#The advantage of this way of writing is that it is easier to understand and easy to operate.
The disadvantages are: the designed program structure is too simple, there is no cache layer object designed, and the program cannot be expanded and maintained well. In other words, this design scheme encapsulates the product (ie: house) and the process of creating the product (ie: house building process), and the coupling is enhanced.
Solution: Decouple the product and product building process => Builder pattern.
2. What is builder pattern?
Builder Pattern (Builder Pattern), also called generator pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract category), making this abstract Different implementation methods of the process can construct objects with different performances (properties).
The builder pattern is to create a complex object step by step. It allows the user to only specify the type of the complex object. and content can be built without the user needing to know the specific construction details inside.
And there are four major roles in the builder mode:
Product (product role): A specific product object.
-
Builder (abstract builder): Creates an interface/abstract class specified by each component of a Product object.
ConcreteBuilder (concrete builder): implements the interface, builds and assembles various components.
Director (director): Constructs an object using the Builder interface . It is mainly used to create a complex object. It mainly has two functions, one is: Isolating the production process of the customer and the object, the other is: Responsible for controlling the production process of the product object.
3.Case code
If the above case of building a house is solved using the builder pattern, then the class diagram is as follows. (The four major roles are all in it), and the House class is As for the specific product (the entity of the house we want to build), HouseBuilder is an abstract builder. The specific construction process is not implemented internally, but is completed by several subclasses below it. These subclasses are the concrete builder (CommonHouse , HighBuilding), the commander is HouseDirector, which is responsible for the construction process of product objects (what type of house do I want to build). The final 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; //抽象的建造者 public abstract class HouseBuilder { protected House house = new House(); //将建造的流程写好, 抽象的方法 public abstract void buildBasic(); public abstract void buildWall(); public abstract void buildRoof(); //建造房子好, 将产品(房子)返回 public House buildHouse() { return house; } }
package com.szh.builder; public class CommonHouse extends HouseBuilder { @Override public void buildBasic() { System.out.println(" 普通房子打地基5m.... "); } @Override public void buildWall() { System.out.println(" 普通房子砌墙10cm.... "); } @Override public void buildRoof() { System.out.println(" 普通房子添加屋顶.... "); } }
package com.szh.builder; public class HighHouse extends HouseBuilder { @Override public void buildBasic() { System.out.println(" 高楼打地基100m.... "); } @Override public void buildWall() { System.out.println(" 高楼砌墙20cm.... "); } @Override public void buildRoof() { System.out.println(" 高楼添加透明屋顶.... "); } }
package com.szh.builder; //指挥者,这里去指定制作流程,返回产品 public class HouseDirector { HouseBuilder houseBuilder; //构造器传入 houseBuilder public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //通过setter传入 houseBuilder public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //如何处理建造房子的流程,交给指挥者 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) { //盖普通房子 CommonHouse commonHouse = new CommonHouse(); //准备创建房子的指挥者 HouseDirector houseDirector = new HouseDirector(commonHouse); //完成盖房子,返回产品(普通房子) houseDirector.constructHouse(); System.out.println("--------------------------"); //盖高楼 HighHouse highHouse = new HighHouse(); //重置建造者 houseDirector.setHouseBuilder(highHouse); //完成盖房子,返回产品(高楼) houseDirector.constructHouse(); } }
3.Builder pattern in JDK
Let’s take a look at the StringBuilder class, its parent class, and the related interfaces implemented by the parent class.
- ##Appendable interface defines multiple append methods (abstract methods) , that is, Appendable is an abstract builder and defines abstract methods.
- AbstractStringBuilder implements the Appendable interface method. The AbstractStringBuilder here is already a builder, but it cannot be instantiated.
- StringBuilder acts as a commander and a specific builder. The implementation of the construction method is completed by AbstractStringBuilder, and StringBuilder inherits AbstractStringBuilder.
- The client (the user program) does not need to know the details of the internal composition of the product, and the product itself and the product The creation process is decoupled 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 easy to replace specific builders or add new specific builders. Users use different The specific builder can get different product objects.
- allows for more granular control over the product creation process. Breaking down the creation steps of complex products into different methods makes the creation process clearer and makes it easier to use programs to control the creation process.
- Adding new concrete builders does not require modifying the code of the original class library. The commander class is programmed for the abstract builder class. The system is easy to expand and complies with the "opening and closing principle".
The products created by the builder mode generally have more in common and their components are similar. If the differences between the products are large, it is not suitable to use the builder mode, so it is used The scope is subject to certain restrictions.
If the internal changes of the product are complex, it may result in the need to define many specific builder classes to implement such changes, causing the system to become very large. Therefore, in this case, it is necessary to Consider whether to choose builder mode.
The abstract factory pattern implements the creation of product families. A product family is a series of products: product combinations with different classification dimensions. Using the abstract factory pattern does not require concern about the construction process. Just care about which products are produced by which factories. The builder mode requires building a product according to a specified blueprint. Its main purpose is to produce a new product by assembling spare parts.
The above is the detailed content of What is Java Builder Pattern. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
