Previous articles on design patterns:

Singleton Patterns — 8 Implementations

Factory Patterns: Factory Method Patterns and Abstract Factory Patterns

Prototype Mode: Quickly copying existing Instances to create new ones

These design patterns can all be classified as object creation patterns. Today we’ll talk about the last object creation Pattern: the Builder Pattern.

The definition of Builder pattern: To separate the construction of a complex object from its representation, so that the same construction process can create different representations

The core of the Builder pattern is to decouple the product from the product building process

The Universal Builder model

The Builder pattern is generally suitable for scenarios that create complex, multi-part objects. With the Builder pattern, the process of building complex objects can be abstracted (abstract categories) without the client knowing the details of product construction.

The builder mode is generally composed of four roles: product, abstract builder, concrete builder and commander.

  • Product (Product role) : A concrete, constructed complex Product object.
  • Builder: Creates the interface/abstract class specified by the various parts of a Product object.
  • ConcreteBuilder: Implements interfaces, builds and assembles components.
  • Director: Builds an object that uses the Builder interface. It is primarily used to create a complex object. It mainly has two functions, one is: isolated the production process of the customer and the object, the other is: responsible for controlling the production process of the product object.

The following is a concrete scenario to illustrate the Builder pattern:

Now we’re going to create a bike.

A bicycle is made up of several parts, usually wheels, pedals, body, seat, and rider’s handlebar

We can also build different types of bikes, such as mountain bikes, shared bikes (Green orange bikes) and so on

Product Role (bicycle) :

Public class Bicycle {// private String wheel; // pedal; // Private String crossbar; // Saddle; // Private String handlebars; @override public String toString() {return "Bicycle{" + "wheel='" + wheel + '' + ", pedal='" + pedal + ''' + ", crossbar='" + crossbar + ''' + ", saddle='" + saddle + ''' + ", handlebars='" + handlebars + ''' + '}'; }}Copy the code

Abstract Builder (Bicycle Builder)

public interface BicycleBuilder {

    void builtWheel(String wheel);

    void builtPedal(String pedal);

    void builtCrossbar(String crossbar);

    void buildSaddle(String saddle);

    void buildHandlebars(String handlebars);

    Bicycle build();
}
Copy the code

Specific Builder (Green Orange Bike Builder) (not advertising, just because I rode green Orange tonight)

public class DiDiBicycleBuilder implements BicycleBuilder { private Bicycle bicycle; public DiDiBicycleBuilder() { bicycle = new Bicycle(); } @Override public void builtWheel(String wheel) { this.bicycle.setWheel(wheel); } @Override public void builtPedal(String pedal) { this.bicycle.setPedal(pedal); } @Override public void builtCrossbar(String crossbar) { this.bicycle.setCrossbar(crossbar); } @Override public void buildSaddle(String saddle) { this.bicycle.setSaddle(saddle); } @Override public void buildHandlebars(String handlebars) { this.bicycle.setHandlebars(handlebars); } @Override public Bicycle build() { return bicycle; }}Copy the code

Director (Bike builder Director)

public class BicycleDirector { private BicycleBuilder builder; public BicycleDirector(BicycleBuilder builder) { this.builder = builder; } public Bicycle builtBicycle(String wheel, String pedal, String crossbar, String saddle, String handlebars) { builder.builtWheel(wheel); builder.builtPedal(pedal); builder.builtCrossbar(crossbar); builder.buildSaddle(saddle); builder.buildHandlebars(handlebars); return builder.build(); }}Copy the code

Client that uses builder mode to create bike objects

public class Main { public static void main(String[] args) { BicycleBuilder builder = new DiDiBicycleBuilder(); BicycleDirector director = new BicycleDirector(builder); Bicycle = director. BuiltBicycle (" Tangerine wheel ", "tangerine pedal "," Tangerine body ", "comfortable seat "," tangerine rider "); System.out.println(bicycle); }}Copy the code

Chain calls build objects

The common chain calls to create objects also leverage the Builder pattern. In this Builder mode, there is no specific role, but the creation process is encapsulated in the Builder Builder.

Specific code:

Public class Bicycle {// private String wheel; // pedal; // Private String crossbar; // Saddle; // Private String handlebars; private Bicycle(BicycleBuilder bicycleBuilder) { this.wheel = bicycleBuilder.wheel; this.pedal = bicycleBuilder.pedal; this.crossbar = bicycleBuilder.crossbar; this.saddle = bicycleBuilder.saddle; this.handlebars = bicycleBuilder.handlebars; } // Constructor, static inner class. Public static class BicycleBuilder{// private String wheel; // pedal; // Private String crossbar; // Saddle; // Private String handlebars; public BicycleBuilder wheel(String wheel){ this.wheel = wheel; return this; } public BicycleBuilder pedal(String pedal) { this.pedal = pedal; return this; } public BicycleBuilder crossbar(String crossbar) { this.crossbar = crossbar; return this; } public BicycleBuilder saddle(String saddle) { this.saddle = saddle; return this; } public BicycleBuilder handlebars(String handlebars) { this.handlebars = handlebars; return this; } public Bicycle built() { return new Bicycle(this); } } @Override public String toString() { return "Bicycle{" + "wheel='" + wheel + ''' + ", pedal='" + pedal + ''' + ", crossbar='" + crossbar + ''' + ", saddle='" + saddle + ''' + ", handlebars='" + handlebars + ''' + '}'; }}Copy the code

Client use:

public class Main { public static void main(String[] args) { Bicycle bicycle = new Bicycle.BicycleBuilder().wheel().crossbar().handlebars().built(); BicycleBuilder().bicycle. System.out.println(bicycle); Bicycle{wheel=' mountain bike wheel ', pedal=' NULL ', crossbar=' stainless steel car ', saddle='null', handlebars=' pressure bike '}Copy the code