1 Builder schema concept
1.1 Introduction
Builder pattern is a step-by-step creation pattern for complex objects, which allows users to control the object's construction process more finely without knowing the details of the internal construction. The purpose of this model is to decouple the process of building complex objects from its components, so as to isolate the construction process from the representation of components.
Builder model is a clear division of labor, an abstract builder, a specific builder, a commander, of course, also need specific products. So let's take a software product as an example: the technical director is the abstract builder, he communicates with the product manager and knows what kind of product to be made; the programmer is the specific worker, and the technical director says you can do it; and the commander is the product manager of the company, responsible for communicating with users and understanding customers. Demand.
1.2 Definition
Separating the construction of a complex object from its representation enables the same construction process to create different representations.
1.3 Use scenarios
- The same method, different execution order, produce different event results;
- Multiple components or parts can be assembled into an object, but the results are different.
- The product class is very complex, or the different order of invocation in the product class produces different efficiencies, so it is very appropriate to use the builder model at this time.
- Initialization of an object is particularly complex when there are many parameters and many parameters have default values.
2 Builder Model UML Class Diagram Universal
Role introduction:
- Product - Product class: abstract class of product.
- Builder - Abstract class, which regulates the formation of products, is generally realized by subclasses of specific component processes.
- ConcreteBuilder - Specific builder.
- Director - Commander, unified assembly process (omitted).
3 General pattern code
(1) Product category
/**
* Product Category
*/
public class Product {
public void doSomething() {
// Independent Business Processing
}
}
(2) Abstract constructor class
/**
* Abstract builder
* If there are multiple product classes, there are several specific builders, and these product classes have the same interface or abstract class.
*/
public abstract class Builder {
// The setPart method is the configuration of parts, setting different parts of a product, or different assembly sequence to produce different products.
public abstract void setPart();
// Construction products
public abstract Product buildProduct();
}
(3) Specific constructors
/**
* ConcreteBuilder
*/
public class ConcreteProduct extends Builder {
private Product product = new Product();
// Setting up product parts
public void setPart() {
// Logical Processing in Product Class
}
// Build a product
public Product buildProduct() {
return product;
}
}
(4) Commander (Director) Category
/**
* Commander class
* Commander class acts as encapsulation to avoid high-level modules penetrating into the implementation class within the builder.
*/
public class Director {
// Building different products
private Builder builder = new ConcreteProduct();
public Product getAProduct() {
builder.setPart();
// Setting up different parts to produce different products
return builder.buildProduct();
}
}
4 Builder Mode Actual Warfare
(1) Product Category: Exploring App
/**
* Product Category: Exploring App
*/
public class Product {
public static final String ANDROID = "android";
public static final String IOS = "ios";
private String appName;
private String appFuction;
private String appSystem;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppFuction() {
return appFuction;
}
public void setAppFuction(String appFuction) {
this.appFuction = appFuction;
}
public String getAppSystem() {
return appSystem;
}
public void setAppSystem(String appSystem) {
this.appSystem = appSystem;
}
@Override
public String toString() {
return "Product [appName=" + appName + ", appFuction=" + appFuction
+ ", appSystem=" + appSystem + "]";
}
}
(2) Abstract builder class - Technical Supervisor (omitable)
/**
* Abstract builder class - Technical Director
*/
public abstract class Build {
public abstract Build setAppName(String appName);
public abstract Build setAppFuction(String appFuction);
public abstract Build setAppSystem(String appSystem);
public abstract Product build();
}
(3) Specific Builder Class - Full Stack Programmer
/**
* Specific Builder Class - Full Stack Programmer
*/
public class WorkBuilder extends Build {
private Product product;// Product Category
private InnerProduct innerProduct = new InnerProduct();// Product Buffer Category
@Override
public Build setAppName(String appName) {
innerProduct.setAppName(appName);
return this;
}
@Override
public Build setAppFuction(String appFuction) {
innerProduct.setAppFuction(appFuction);
return this;
}
@Override
public Build setAppSystem(String appSystem) {
innerProduct.setAppSystem(appSystem);
return this;
}
@Override
public Product build() {
product = new Product();
product.setAppName(innerProduct.getAppName());
product.setAppFuction(innerProduct.getAppFuction());
product.setAppSystem(innerProduct.getAppSystem());
return product;
}
/**
* Product Buffer Category
*/
private class InnerProduct {
private String appName;
private String appFuction;
private String appSystem;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppFuction() {
return appFuction;
}
public void setAppFuction(String appFuction) {
this.appFuction = appFuction;
}
public String getAppSystem() {
return appSystem;
}
public void setAppSystem(String appSystem) {
this.appSystem = appSystem;
}
}
}
(4) Commander category - Product Manager (omitable)
/**
* Commander Category - Product Manager
*/
public class Director {
public static Product create(String system) {
return new WorkBuilder().setAppSystem(system).setAppName("Exploration").setAppFuction("Look for your sister in the same row").build();
}
}
(5) Client-Client
/**
* Client-Client
*/
public class Client {
public static void main(String[] args){
// Customer: I need a software that can shake me to find my sister.
// Product Manager: Make a probe after analysis.
// Technological Supervisor: appName: Exploration, System: ios, android Function: Shake to find a sister
Product android = Director.create(Product.ANDROID);
Product ios = Director.create(Product.IOS);
System.out.println(android);
System.out.println(ios);
/**
* Variant builders can only need specific builders, Abstract builders and commanders.
*/
// The programmer felt too tired, paid less and did the most, and finally decided to go out and do it alone.
WorkBuilder niubiProgremer = new WorkBuilder();
Product androidBest = niubiProgremer.setAppName("Exploration").setAppSystem(Product.ANDROID).setAppFuction("Shake and find your sister").build();
Product iosBest = niubiProgremer.setAppName("Exploration").setAppSystem(Product.IOS).setAppFuction("Shake and find your sister").build();
System.out.println("-------------------------------------------------------------------------------------");
System.out.println(androidBest);
System.out.println(iosBest);
}
}
(6) Results
Product [appName = Exploration, appFuction = Uniform Search for Girls, appSystem=android] Product [appName = Exploration, appFuction = Uniform Search for Girls, appSystem=ios] ------------------------------------------------------------------------------------- Product [appName = exploration, appFuction = shake, find a sister, appSystem=android] Product [appName = exploration, appFuction = shake, find a sister, appSystem=ios]
Builder mode in 5 Android source code
In Android source code, the most commonly used Builder pattern is AlertDialog.Builder, which is used to build complex AlertDialog objects.
6 Summary
Builder mode is also commonly used in Android development, usually as a builder of configuration classes to separate the construction and presentation of configuration, but also to isolate configuration from the target class to avoid too many setter methods. The common implementation of Builder mode is through call chains, which makes the code more concise and understandable. For example, the image Loader framework is configured through ImageLoader Config, which avoids too much interface "contamination" in the target class.
6.1 Advantages
(1) Good encapsulation, the use of builder mode can make the client do not need to know the details of the internal composition of the product;
(2) The builder is independent and easy to expand;
(3) Some other objects in the system will be used in the process of object creation, which are not easy to obtain in the process of product object creation.
6.2 Disadvantage
(1) Redundant Builder objects and Director objects will be generated, which consumes memory;
(2) Exposure of object construction process.
7 Reference Articles and Links
Android Source Code Design Patterns Analysis and Practice
Zen of Design Patterns
Builder Mode of Android Source Analysis
Source Analysis of Android Advanced Series Alert Dialog Builder Model