Design Patterns article

The mediator pattern

The proxy pattern

Abstract Factory Pattern details – Head First design pattern

Decorator pattern

Adapter mode

The strategy pattern

Observer model

Builder Mode

Builder model

The Builder Pattern uses multiple simple objects to build a complex object step by step. This type of design pattern is the creation pattern, which provides the best way to create objects.

A Builder class constructs the final object step by step. The Builder class is independent of other objects.

introduce

Intent: Separate a complex build from its representation so that the same build process can create different representations.

Main solution: main solution in the software system, sometimes faced with “a complex object” to create work, which is usually composed of each part of the sub-object with a certain algorithm; The pieces of this complex object often face drastic changes due to changing requirements, but the algorithms that put them together are relatively stable.

When to use: When some basic components do not change, but the combination often changes.

How to solve it: Separate change from immobility.

Key code: Builder: creates and provides instances, director: manages the dependencies of built instances.

1, go to KFC, hamburger, Coke, French fries, fried chicken wings and so on are unchanged, but their combination is often changed, generating the so-called “set meal”. StringBuilder in JAVA.

Advantages: 1, independent builder, easy to expand. 2, easy to control the details of risk.

Disadvantages: 1, the product must have something in common, the scope is limited. 2. If the internal changes are complex, there will be many construction classes.

Usage scenarios: 1. The object to be generated has a complex internal structure. 2. The internal properties of the object to be generated depend on each other.

Note: The difference from factory mode is that builder mode is more concerned with the order in which parts are assembled.

Builder pattern UML class diagram generic

Role Introduction:

  • Product — Product class: An abstract class for a Product.
  • Builder — An abstract class that regulates the building of a product, typically subclasses that implement concrete component processes.
  • ConcreteBuilder — a ConcreteBuilder.
  • Director — the Director who unites the assembly process.

 

 

 

The generic type codes are as follows:

(1) Product category

The methods and attributes of a product class are used to add attributes to the product itself. Some products need multiple attributes, while others may have only one attribute. The following code is just an example, and other attributes can be added in this way.

/** * Product class */ public class Product {protected String name; public void setName(String name) { this.name = name; }}Copy the code

(2) Abstract builder class

Correspond to the attributes of the product and provide methods for them. This is like static proxy mode, where the proxy class Builder is used to generate the proxied object Product.

/** * Abstract builder * If there are multiple product classes there are several concrete builders, Public abstract class Builder {public abstract void setName(String name); public abstract void setName(String name); Public abstract Product create(); }Copy the code

(3) Specific builder class

Each specific Builder has its own unique attributes and is a proxy for a specific Product.

Public class ConcreteBuilder extends Builder {private ConcreteBuilder = new ConcreteBuilder (); @override public void setName(String name) {product.setName(name); } @Override public Product create() { return product; }}Copy the code

(4) Director

Specific products are generated through the Director. The ConcreteBuilder method returns this for each ConcreteBuilder, and then calls the Create method. In this way, the Director class is omitted and the logic is clearer.

Public class Director {private Builder mBuilder = null; private Builder mBuilder = null; Public Director(Builder Builder) {mBuilder = Builder; } public Product getAProduct(String name) {mBuilder.setName(name); Return mBuilder.create(); return mBuilder.create(); }}Copy the code

Other related patterns

Consider using the Builder pattern when encountering multiple constructor parameters, as mentioned in Effective Java Version 2. The Objects implemented by the Builder pattern are more convenient to use than the overlapping Constructor pattern and JavaBeans pattern.

1 Overlapping constructor pattern

In this mode, you provide the first constructor with only required parameters, the second constructor with one optional parameter, the third constructor with two optional parameters, and so on, and the last constructor with all optional parameters. Let’s look at the programming implementation:

/** * public class Person {private final int id; private final String name; // Optional parameter Private final int age; private final String sex; private final String phone; private final String address; private final String desc; public Person(int id, String name) { this(id, name, 0); } public Person(int id, String name, int age) { this(id, name, age, ""); } public Person(int id, String name, int age, String sex) { this(id, name, age, sex, ""); } public Person(int id, String name, int age, String sex, String phone) { this(id, name, age, sex, phone, ""); } public Person(int id, String name, int age, String sex, String phone, String address) { this(id, name, age, sex, phone, address, ""); } public Person(int id, String name, int age, String sex, String phone, String address, String desc) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.phone = phone; this.address = address; this.desc = desc; }}Copy the code

From the above code, when you want to create an instance, use the constructor with the shortest argument list, but that list contains all the arguments to set:

Person Person = new Persion(1, "li Si ", 20," male ", "18800000000", "China", "test using overlapping constructor mode ");Copy the code

This constructor call usually requires a lot of parameters that you don’t want to set, but you still have to pass values for them.

Bottom line: Overlapping constructors work, but when there are many arguments, the code to create usage can be hard to write and harder to read.

2 the JavaBeans model

There is a second alternative when many constructor parameters are encountered, which is the JavaBeans pattern. In this mode, a no-argument constructor is called to create the object, and then setter methods are called to set each required argument, along with each associated optional argument:

/** * public class Person {private int id; private String name; // Optional parameter private int age; private String sex; private String phone; private String address; private String desc; public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSex(String sex) { this.sex = sex; } public void setPhone(String phone) { this.phone = phone; } public void setAddress(String address) { this.address = address; } public void setDesc(String desc) { this.desc = desc; }}Copy the code

Create each required object:

Person p1=new Person(); Person p2=new Person(" Person "); Person p3=new Person(" Person ",18); Person p4=new Person(" wang5 ",21,180); Person p5=new Person(" zhao ",17,170,65.4);Copy the code

If you don’t click on the source code, you don’t know which one is weight and which one is height. Another problem is that writing this constructor can be very cumbersome when there are many arguments. If you try Builder mode from a different Angle, you will find that the code becomes much more readable.

 

By comparing it with the other two patterns, we can see more clearly what is good about the Builder pattern and why it is designed this way.

  

reference

1. Builder mode

2. Builder mode of creation design mode

3, Java Efficient programming Builder mode