Builder pattern
Definition and characteristics of the pattern
Definition of Builder pattern: refers to separating the construction of a complex object from its representation so that the same construction process can create different representations. This design pattern is called the Builder pattern. It breaks down a complex object into multiple simple objects and then builds it step by step. It separates change from immutability, that is, the components of the product remain unchanged, but each part can be flexibly selected.
The main advantages of this model are as follows:
Each specific builder is independent of each other, which is conducive to system expansion. The client does not need to know the details of the internal composition of the product, which facilitates control of detailed risks.
The disadvantages are as follows:
The components of the product must be the same, which limits its scope of use. If the internal changes of the product are complex, this pattern will add a lot of builder classes.
The builder pattern and the factory pattern have different focuses: the builder pattern focuses on the assembly process of components, while the factory method pattern focuses more on the creation process of components, but the two can be used in combination.
(Recommended video: java videotutorial)
The structure and implementation of the pattern
The Builder pattern is composed of It consists of four elements: product, abstract builder, concrete builder, and commander. Now let's analyze its basic structure and implementation method.
1. Structure of the pattern
The main roles of the Builder pattern are as follows.
Product role (Product): It is a complex object containing multiple components, and its various components are created by specific builders. Abstract builder (Builder): It is an interface that contains abstract methods for creating various subcomponents of a product, and usually also contains a method getResult() that returns complex products. Concrete Builder: Implements the Builder interface to complete the specific creation methods of each component of a complex product. Director: It calls the component construction and assembly methods in the builder object to complete the creation of complex objects. The director does not involve specific product information.
The structure diagram is as follows:
2. Implementation of the pattern
(1) Product role : A complex object containing multiple component parts.
public class Product { private String partA; private String partB; private String partC; public void setPartA(String partA) { this.partA = partA; } public void setPartB(String partB) { this.partB = partB; } public void setPartC(String partC) { this.partC = partC; } public void show() { //显示产品的特性 } }
(2) Abstract builder: Contains abstract methods for creating various sub-components of the product.
public abstract class Builder { //创建产品对象 protected Product product = new Product(); public abstract void buildPartA(); public abstract void buildPartB(); public abstract void buildPartC(); //返回产品对象 public Product getResult() { return product; } }
(3) Concrete builder: Implements the abstract builder interface.
public class ConcreteBuilder extends Builder { public void buildPartA() { product.setPartA("建造 PartA"); } public void buildPartB() { product.setPartA("建造 PartB"); } public void buildPartC() { product.setPartA("建造 PartC"); } }
(4) Commander: Call methods in the builder to complete the creation of complex objects.
public class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } //产品构建与组装方法 public Product construct() { builder.buildPartA(); builder.buildPartB(); builder.buildPartC(); return builder.getResult(); } }
(5) Client
public class Client { public static void main(String[] args) { Builder builder = new ConcreteBuilder(); Director director = new Director(builder); Product product = director.construct(); product.show(); } }
Application scenarios of the pattern
The Builder pattern creates complex objects, and its products The individual parts often face drastic changes, but the algorithm that combines them is relatively stable, so it is usually used in the following situations.
The created object is more complex and consists of multiple components. Each component faces complex changes, but the construction sequence between components is stable. The algorithms used to create complex objects are independent of the object's component parts and how they are assembled, i.e. the building process and final representation of the product are independent.
This article comes from php Chinese website, java tutorial column, welcome to learn!
The above is the detailed content of Characteristics and application scenarios of java builder pattern. For more information, please follow other related articles on the PHP Chinese website!