Original: Curly brace MC(wechat official account: Huakuohao-MC), welcome to share, please keep the source.
The Builder pattern is used to separate the creation and presentation of complex objects, some of which are made up of many parts, each of which can have many different choices, and often needs to be considered when creating such objects.
For example
A car is composed of engine, steering wheel, lights, lights, body color and so on. The color, wheel size and lamp style of each car may be different, but the components of the car will not be less.
The construction mode can be implemented in two ways, the first is with a director, and the second is without a director. In my experience, daily use of the auteurless approach is probably more likely.
A director,
The so-called director is a director class to direct the object creation process, the client uses the director class to obtain the object, regardless of the specific object creation process.
Take a look at the UML diagram to get a sense of the build pattern.
Let’s look at the code. Let’s take building a car
public class Car {
private String wheel;
private String engine;
private String seat;
private String lamp;
private String color;
//篇幅原因,此处省略get,set方法
@Override
public String toString() {
return "Car{" +
"wheel='" + wheel + ''' +
", engine='" + engine + ''' +
", seat='" + seat + ''' +
", lamp='" + lamp + ''' +
", color='" + color + ''' +
'}';
}
}
Copy the code
Abstract Builder class that specifies the steps to build complex objects
public abstract class Builder {
public abstract void buildWheel();
public abstract void buildSeat();
public abstract void buildLamp();
public abstract void buildColor();
public abstract void buildEngine();
public abstract Car getCar();
}
Copy the code
Concrete Builder class, the realization of complex object concrete construction process and content
public class ConcreteBuilder extends Builder { private Car car; public ConcreteBuilder() { car = new Car(); } @Override public void buildWheel() { car.setWheel("wheel"); } @Override public void buildSeat() { car.setSeat("seat"); } @Override public void buildLamp() { car.setLamp("lamp"); } @Override public void buildColor() { car.setColor("color"); } @Override public void buildEngine() { car.setEngine("engine"); } @override public Car getCar() {return Car; }}Copy the code
The Director class, which determines the creation process of complex objects.
public class CarDirector { public Car createCar(Builder builder){ builder.buildWheel(); builder.buildSeat(); builder.buildLamp(); builder.buildColor(); builder.buildEngine(); return builder.getCar(); }}Copy the code
The client is used this way
public class BuilderClient { public static void main(String[] args){ CarDirector carDirector = new CarDirector(); Car Car = Cardirector.createcar (new ConcreteBuilder()); System.out.println(car.toString()); }}Copy the code
No director
Auteurless mode feels like a lot of everyday development, and whenever you see code that looks like this, it’s most likely builder mode.
Car car = concreteBuilderA.buildEngine("engine")
.buildLamp("lamp")
.buildSeat("seat")
.buildColor("color")
//.buildWheel("wheel")
.build();
Copy the code
Let’s take a look at the UML diagram for a general idea.
Again, let’s look at the code implementation, again creating a Car as an example, so the code for Car is not given again.
Builder class
Public abstract class BuilderA {// Return BuilderA buildWheel(String wheel); abstract BuilderA buildEngine(String engine); abstract BuilderA buildLamp(String lamp); abstract BuilderA buildSeat(String seat); abstract BuilderA buildColor(String color); abstract Car build(); }Copy the code
Concrete Builder, responsible for the specific creation of objects.
public class ConcreteBuilderA extends BuilderA { private Car car; public ConcreteBuilderA() { car = new Car(); } @Override BuilderA buildWheel(String wheel) { car.setWheel(wheel); return this; } @Override BuilderA buildEngine(String engine) { car.setEngine("engine"); return this; } @Override BuilderA buildLamp(String lamp) { car.setLamp("lamp"); return this; } @Override BuilderA buildSeat(String seat) { car.setSeat("seat"); return this; } @Override BuilderA buildColor(String color) { car.setColor("color"); return this; } @Override Car build() { return car; }}Copy the code
The client is used this way
public class BuilderAClient { public static void main(String[] args){ ConcreteBuilderA concreteBuilderA = new ConcreteBuilderA(); Car car = concreteBuilderA.buildEngine("engine") .buildLamp("lamp") .buildSeat("seat") .buildColor("color") //.buildWheel("wheel") .build(); System.out.println(car.toString()); }}Copy the code
conclusion
The Builder pattern is one of the creation patterns. The so-called no Director pattern simply leaves the construction process to the client and lets the user decide how to create objects. The key to the realization of the directorless mode is that the method of building each component in the Builder class is to return the Builder itself.
Recommended reading
1. Java concurrent programming stuff (10) — Final summary
2. Common network problem locating tools that programmers should master
3. Do you know how to use Awk
4. Teach you how to build a set of ELK log search operation and maintenance platform
Original: Curly brace MC(wechat official account: Huakuohao-MC) Focus on JAVA basic programming and big data, focus on experience sharing and personal growth.