- Definition: To separate the construction of a complex object from its representation, so that the same construction process can create different representations.
- Usage scenarios: (1). The same method, different execution sequence, different event results. (2). Multiple components or parts can be assembled into an object, but the resulting operation results do not want to be at the same time. (3). The constructor pattern is appropriate when the product class is very complex, or when the order of calls in the product class has different effects. (4). When the initialization of an object is particularly complex, such as multiple parameters, many of which have default values.
- Code implementation:
/*** * Computer abstraction class **@author ping * */ public class Computer { protected String mBoard; protected String mDisplay; protected String mOS; / / system public void setmBoard(String mBoard) { this.mBoard = mBoard; } public void setmDisplay(String mDisplay) { this.mDisplay = mDisplay; } public String getmOS(a) { return mOS; } public void setmOS(String mOS) { this.mOS = mOS; } public String getmBoard(a) { return mBoard; } public String getmDisplay(a) { return mDisplay; } public void setOs(String os) { this.mOS = os; } // Build the Builder class public static class Builder { private Computer mComputer; public Builder(a) { super(a); mComputer =new Computer(); } public Builder builderBoard(String board) { mComputer.setmBoard(board); return this; } public Builder builderDisplay(String display) { mComputer.setmDisplay(display); return this; } public Builder builderOS(String os) { mComputer.setOs(os); return this; } public Computer create(a) { returnmComputer; }}@Override public String toString(a) { return "Computer [mBoard=" + mBoard + ", mDisplay=" + mDisplay + ", mOS=" + mOS + "]"; }}Copy the code
Test code:
public class BuildrtTest {
/ * * *@param args
*/
public static void main(String[] args) {
Computer.Builder mBuilder = new Computer.Builder().builderBoard(Intel motherboard).builderDisplay("Retina Display").builderOS("Mac OS X 10.11");
System.out.print("\nComputer info: " + mBuilder.create().toString());
Computer mComputer = new Computer.Builder().builderBoard(Intel motherboard).builderDisplay("Retina Display").builderOS("Mac OS X 10.11").create();
System.out.print("\nComputer info: " + mComputer.getmBoard() +"-" +mComputer.getmDisplay() +"-"+ mComputer.getmOS()); }}Copy the code
Output print:
Computer Info: Computer [mBoard= Intel, mDisplay=Retina display, mOS=Mac OS X 10.11] Intel motherboard –Retina Display –Mac OS X 10.11
- Summary: In Android development, the Builder pattern is usually used as the Builder of the configuration class to separate the construction and presentation of the configuration, and also to isolate the configuration from the target class, avoiding excessive setter methods. Builder mode is more common implementation through the call chain implementation, making the code more brief, easy to understand.
- Advantages:
- Good encapsulation, the use of Builder mode can make the client do not have to know the internal composition of the product details;
- The builder is independent and easy to expand;
- Disadvantages:
- Generate redundant Builder objects, consuming memory