1. What is builder mode?
We know that in the software development process, sometimes we need 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 ourselves, we need to assemble it from CPU, motherboard, memory, hard disk, graphics card, chassis, monitor, keyboard, mouse and other components. For example, if a school needs to purchase 100 computers, it is impossible for the school to buy the parts and assemble them by itself. It must tell the buyer what configuration it needs, and then the buyer will go to the computer company to buy them. The company will assemble them for you and then hand them over to the buyer. , and finally handed it over to the school.
As can be seen from the above example, each component can be flexibly selected (including the level of various computer configurations), but the creation steps are similar. However, the creation of this type of product cannot be described by the factory pattern introduced earlier. Only the builder pattern can well describe the creation of this type of product.
2. Definition of Builder Pattern
Definition of Builder Pattern:
Separates the construction of a complex object from its representation so that the same construction process can create Differently represented, such a 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.
Summary in one sentence: Separate the construction of a complex object from its representation, so that the same construction process can create different representations
Type: Create a class pattern
3. Advantages and Disadvantages of Builder Pattern
Advantages:
Good encapsulation, separation of construction and presentation
Extensibility Better, each specific builder is independent of each other, which is conducive to system decoupling
The client does not need to know the details of the internal composition of the product, the builder can gradually refine the creation process, Without having any impact on other modules, it is easy to control detailed risks
Disadvantages:
The components of the product must be the same, which limits its use Scope
If the internal changes of the product are complex, if the internal changes of the product occur, the builder will also have to make simultaneous modifications, and the later maintenance cost will be larger
Note:
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.
4. The structure of the builder pattern
The main roles of the builder pattern are as follows:
Product role (Product): It contains multiple Complex objects that make up components, each component is created by a specific builder
Abstract Builder (Builder): Implements the Builder interface to construct and assemble the various components of the product, defined And clarify the representation it creates, and provide an interface for retrieving products
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. The director does not involve specific product information
Structural diagram (referenced from the Internet):
5. Builder mode code demonstration
For example, now we need to build a car:
Product category:
/** * 产品类 */ public class Car { //车的建造过程 private String wheel; //车轮 private String skeleton; //车架 private String 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 category:
/** * 指导者 */ //汽车总监 public class CarDirector { public Car constructCar(ICarBuilder builder){ builder.buildwheel(); builder.buildSkeleton(); builder.buildEngine(); return builder.buildCar(); } }
Concrete builder:
/** * 具体建造者:生产具体的东西 */ public class ConcreteBuilder implements ICarBuilder{ Car car; public ConcreteBuilder(){ car = new Car(); } @Override public void buildwheel() { car.setWheel("轮子"); } @Override public void buildSkeleton() { car.setSkeleton("车身结构"); } @Override public void buildEngine() { car.setEngine("发动机"); } @Override public Car buildCar() { return this.car; } }
Abstract builder (interface):
/** * 抽象建造者 */ public interface ICarBuilder { public void buildwheel(); //构建车轮 public void buildSkeleton(); //构建骨架 public void buildEngine(); //构建发动机 Car buildCar(); }
Test category:
public class Test { public static void main(String[] args){ //新建一个总监(替你去买汽车) 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 combining it with the first part of the article, you can understand it! !
6. Application scenarios of the builder pattern
The only difference between the builder pattern and the factory pattern is the creation of complex objects. In other words, if you create simple objects, you usually use the factory pattern to create them, and if you create complex objects, you can consider using the builder pattern
Main application scenarios:
-
The same method, different execution order, produces different results
Multiple components or parts can be assembled into one object, but the results produced are different
The product class is very complex, or different calling sequences in the product class produce different effects
Initializing an object is particularly complex, with many parameters, and Many parameters have default values
7. The difference between builder mode and factory mode
Builder mode pays more attention to the calling order of methods. The factory pattern focuses on creating objects
-
The intensity of creating objects is different. The builder pattern creates complex objects, which are composed of various complex components. The objects created by the factory pattern are all the same
The focus is different. The factory mode only needs to create the object, while the builder mode not only needs to create the object, but also knows what parts the object is composed of
The builder mode is different according to the order in the construction process, and the final object component composition is also different
The above is the detailed content of What is the builder pattern in Java design patterns?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
