- Thank you very much for reading this article. Welcome to 👍
- It’s not hard to give up, but it must be cool to insist! I hope all of us can make progress every day! 🎉
What is the Builder pattern
A Builder Pattern is a type of design Pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
Practical scope:
- When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled.
- When the construction process must allow different representations of the objects being constructed.
Design patterns have nothing to do with programming languages, but most of them use the Java language for practical examples.
The role in builder mode
- Builder: Specifies the abstract interface for the parts that create a product object.
- ConcreteBuilder: Implement the Builder’s interface to construct and assemble the components of the product, define and specify the representation it creates, and provide an interface to retrieve the product.
- Director: Constructs an object that uses the Builder interface.
- Product: Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the classes that define the component parts and the interfaces that assemble those parts into the final product.
Product Role
We use the car as the Product role. The car here contains the body, the engine and the power source (of course, the real car contains more than these parts), the body must have, without the body is not a car at all.
package com.secondgod.builder;
import java.text.MessageFormat;
/**
* 汽车
*
* @authorThe white hat of the two masters https://le-yi.blog.csdn.net/ */
public class Car {
/** * body (must have) */
private final String body;
/** * engine */
private String engine;
/** * Power source */
private String energy;
public Car(String body) {
this.body = body;
}
public String getBody(a) {
return body;
}
public String getEngine(a) {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getEnergy(a) {
return energy;
}
public void setEnergy(String energy) {
this.energy = energy;
}
public String toString(a) {
return MessageFormat.format("A vehicle equipped with the [{1}] engine and the [{2}] power source on the [{0}] body.", body, engine, energy); }}Copy the code
Abstract Builder
We use the automobile line interface as the abstract Builder role. Declare the way the automobile production line should be. Here we just include the way to build the body, the way to build the engine, the way to build the power source, the parts for the car, and a way to get the car to take it off the production line.
package com.secondgod.builder;
/** * Automobile production line **@authorThe white hat of the two masters https://le-yi.blog.csdn.net/ */
public interface ICarBuilder {
/**
* 建造车身
*/
void buildBody(a);
/** * Build engine */
void buildEngine(a);
/** * Construction power source */
void buildEnergy(a);
/** * Get the car *@return* /
Car retrieveCar(a);
}
Copy the code
ConcreteBuilder role
We use the fuel car line and the electric car line as the ConcreteBuilder. Here is a detail, since the body is necessary, the body must be built first.
package com.secondgod.builder;
/** * Fuel truck production line **@authorThe white hat of the two masters https://le-yi.blog.csdn.net/ */
public class GasolineCarBuilder implements ICarBuilder {
private Car car;
@Override
public void buildBody(a) {
car = new Car("Fuel");
}
@Override
public void buildEngine(a) {
car.setEngine("Fuel engine");
}
@Override
public void buildEnergy(a) {
car.setEnergy("Gasoline tank");
}
@Override
public Car retrieveCar(a) {
Car retrieveCar = car;
car = null;
returnretrieveCar; }}Copy the code
package com.secondgod.builder;
/** * Electric vehicle production line **@authorThe white hat of the two masters https://le-yi.blog.csdn.net/ */
public class ElectricCarBuilder implements ICarBuilder{
private Car car;
@Override
public void buildBody(a) {
car = new Car("Electric");
}
@Override
public void buildEngine(a) {
car.setEngine("Electric drive system");
}
@Override
public void buildEnergy(a) {
car.setEnergy("New Energy Battery");
}
@Override
public Car retrieveCar(a) {
Car retrieveCar = car;
car = null;
returnretrieveCar; }}Copy the code
The role of Director
We use the car engineer as the Director. He knows how to operate the production line (sorry, our production line is not fully automatic).
package com.secondgod.builder;
/** * Automotive engineer **@authorThe white hat of the two masters https://le-yi.blog.csdn.net/ */
public class CarEngineer {
/** * build the car **@param builder
* @return* /
public Car constructCar(ICarBuilder builder) {
builder.buildBody();
builder.buildEngine();
builder.buildEnergy();
returnbuilder.retrieveCar(); }}Copy the code
The ultimate test
package com.secondgod.builder;
public class Test {
public static void main(String[] args) {
// An engineer
CarEngineer carEngineer = new CarEngineer();
// Two production lines
ICarBuilder gasolineCarBuilder = new GasolineCarBuilder();
ICarBuilder electricCarBuilder = new ElectricCarBuilder();
// Two a day
Car gasolineCar = carEngineer.constructCar(gasolineCarBuilder);
Car electricCar = carEngineer.constructCar(electricCarBuilder);
// Enough to live onSystem.out.println(gasolineCar); System.out.println(electricCar); }}Copy the code
As we expected, Nice.
The end of the
The Builder pattern often uses morphs that combine the abstract builder role, the concrete builder role, and the director role, such as the StringBuilder class that comes with the JDK.