Constructor mode, also known as creator mode, separates the construction of a complex object from its representation so that the same construction process can create different representations.Creator mode hides the process of creating complex objects. It abstracts the process of creating complex objects and dynamically creates objects with complex properties by inheriting or overloading subclasses.
It's a bit official. Simply put, there's an object creation process that's complex. We use the constructor model to hide this complex creation process, and we can use the builder to make the object creation more diverse.
For example, the process of creating a computer requires the creation of a series of small objects, such as CPU, hard disk, screen, host, memory, adapter, keyboard, etc. before a computer object can be built, that is, if you create a computer object, you usually write our code like this:
new Computer(new CPU(),new screen (), new hard disk (), new memory (), new host (), new memory (), new adapter ())
This makes it very difficult to create objects. There may also be two CPUs and two memory for computer objects, which makes object creation more complex. The constructor design mode is to solve the creation of complex objects and the diversity of object creation.
1. Builder Role
The process of creating complex objects is abstracted and an abstract interface is given to standardize the construction of each component of a product object.This interface specifies which part of a complex object to create, but it does not involve the creation of specific objects that are not visible.
2. Specific Creator Role (ConcreateBuilder)
Implement the Builder interface to materialize the creation of parts of complex objects for different business logic.After the construction process is complete, provide an instance of the product.
3. Director
Call specific builders to create parts of complex objects. The instructor does not design specific product information, but is responsible for ensuring that the parts of the object are created completely or in some order.
4. Products (Computer)
Complex objects to be created generally contain multiple parts.
Here's what we'll do with the code:
First, we define the Computer class, which stores parts of the Computer object in a List:
class Computer { List<String> parts = new ArrayList<>(); public void show() { for (String part : parts) { System.out.println(part); } } }
Next, we define the Builder abstract class and define Computer as having three parts: CPU, Disk, Screen:
interface Builder { void buildCpu(); void buildDisk(); void buildScreen(); Computer getComputer(); }
The ThinkPadComputerBuilder and DaiErComputerBuilder roles defined below
class ThinkPadComputerBuilder implements Builder { private Computer com = new Computer(); @Override public void buildCpu() { com.parts.add("ThinkPadCPU"); } @Override public void buildDisk() { com.parts.add("ThinkPadDisk"); } @Override public void buildScreen() { com.parts.add("ThinkPadScreen"); } @Override public Computer getComputer() { return com; } } class DaiErComputerBuilder implements Builder { private Computer com = new Computer(); @Override public void buildCpu() { com.parts.add("DaiErCPU"); } @Override public void buildDisk() { com.parts.add("DaiErDisk"); } @Override public void buildScreen() { com.parts.add("DaiErScreen"); } @Override public Computer getComputer() { return com; } }
Below we define the mentor role BuilderDirector, add a reference to Builder to the class, and initialize the builder property through its construction methods:
class Direct { private Buidler builder; public Direct(Builder builder) { this.builder = builder; } public Computer build() { builder.buildCpu(); builder.buildDisk(); builder.buildScreen(); return builder.getComputer(); } }
Write test classes:
public class Test { public static void main(String[] args) { //First, create a build class that specifically creates small objects Buidler thinkPadbuidler = new ThinkPadComputerBuilder(); Buidler daiErBuidler = new DaiErComputerBuilder(); //Pass build class into control class Direct dir1 = new Direct(thinkPadbuidler); Direct dir2 = new Direct(daiErBuidler); //Call the control class, assembly method, and make up a Computer object Computer thinkPadcomputer = dir1.build(); Computer daiErComputer = dir2.build(); thinkPadcomputer.show(); daiErComputer.show(); } }
Summary:
1) Benefits of using the Builder model:
(1) Builder mode allows clients to avoid having to know the details of the internal composition of the product.
(2) Specific builder classes are independent of each other, which is conducive to the expansion of the system.
(3) Specific builders are independent, so they can refine the construction process step by step without affecting other modules.
2) Scenarios for using the Builder Mode:
When creating some complex objects, the order of construction between their internal components is stable, but the construction of their internal components is facing complex changes.
(2) The algorithm of a complex object to be created is independent of its components and of its assembly method.