Builder mode builder
Introduction:
For Android, the Builder design mode may be rarely used in our development, but we may have seen that AlterDialog. Builder is a Builder mode. So what is the builder model? Let's take a look at its standard definition:
Definition:
Separates the construction of a complex object from its representation so that different representations can be created during the same construction process, and each part can be constructed step by step.
It looks abstract and I don't know what it means? The following is an example of how to use code to see what the builder mode is.
Example:
Public class ImageBean {private int id; // image id private String path; // image path private long size; // image size private String type; // image type public ImageBean () {} public ImageBean (int id, String path, long size, String type) {this. id = id; this. path = path; this. size = size; this. type = type;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getPath () {return path;} public void setPath (String path) {this. path = path;} public long getSize () {return size;} public void setSize (long size) {this. size = size;} public String getType () {return type;} public void setType (String type) {this. type = type ;}}
We define an ImageBean object that contains the id, path, size, and type attributes. It also contains an empty constructor and a constructor containing parameters. In this way, we can assign values to this entity when declaring it.
ImageBean ib = new ImageBean (1, "xxxx", "54321", "jpg"); it is better to have fewer parameters for an object. We can remember the constructed parameters, what if this object has N parameters? How do we know the order of parameters? Generation
The code is not highly readable. If it is not written by yourself, it cannot be understood by others. Even if it is written by yourself, I am afraid we will forget it for a long time. Then the Builder mode can solve this problem.
We can create a Builder for ImageBean:
Public class ImageBean {private int id; // image id private String path; // image path private long size; // image size private String type; // image type public ImageBean () {} public ImageBean (Builder builder) {this. id = builder. id; this. path = builder. path; this. size = builder. size; this. type = builder. type;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getPath () {return path;} public void setPath (String path) {this. path = path;} public long getSize () {return size;} public void setSize (long size) {this. size = size;} public String getType () {return type;} public void setType (String type) {this. type = type;} public static class Builder {private int id; private String path; private long size; private String type; public Builder id (int id) {this. id = id; return this;} public Builder path (String path) {this. path = path; return this;} public Builder size (long size) {this. size = size; return this;} public Builder type (String type) {this. type = type; return this;} public ImageBean build () {return new ImageBean (this );}}}
ImageBean.Builder builder = new ImageBean.Builder(); ImageBean ib = builder.id(11).path("xxx").size(1111).type("jpg").build();
We can see that the construction parameter of ImageBean is changed to this builder, and then a Builder with the same value as ImageBean is defined. The return value is a builder object. This is the separation of structure and representation in the definition. The structure is constructed in ImageBean, and the display is displayed in Builder. That is, each part of the distributed structure.
The above is only a usage of the builder design mode.
Summary:
Use Build mode in the following scenarios:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the constructed object.
3 The Builder mode should solve the following problems:
When the objects we want to create are complex (usually composed of many other objects ),
We need to separate the process of creating a complex object from the representation (Display) of this object,
The advantage of doing so is to build complex objects step by step,
Because parameters can be introduced in the construction process of each step, the presentation of objects obtained after the creation of the same step is different.
We can see that the construction parameter of ImageBean is changed to this builder, and then a Builder with the same value as ImageBean is defined. The return value is a builder object. This is the separation of structure and representation in the definition. The structure is constructed in ImageBean, and the display is displayed in Builder. That is, each part of the distributed structure.
The above is a summary of the usage of the builder design model:
Use Build mode in the following scenarios:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the constructed object.
3 The Builder mode should solve the following problems:
When the objects we want to create are complex (usually composed of many other objects ),
We need to separate the process of creating a complex object from the representation (Display) of this object,
The advantage of doing so is to build complex objects step by step,
Because parameters can be introduced in the construction process of each step, the presentation of objects obtained after the creation of the same step is different.