Preface
Builder modeEveryone should be familiar with it. In our coding career, we will always encounter it. This guy is present in both AlertDialog in Android development, OkHttp and Retrofit in network frameworks, or JavaPoet.
The reason why it is so popular is not only because it is relatively difficult to get started, but also because it has indeed solved a problem in our daily development.Too many parameters required when creating an object。
Give a small example
In the past few years, everyone has been popular in cryptocurrency speculation, which has made it difficult to find a card on the market. With the introduction of government policies and the collapse of virtual currencies. Graphics cards are no longer a state of pricelessness. Xiaolong, who just graduated from college, opened a computer store, which specializes in providing computers for people. At the beginning, the requirements were relatively simple, and only recorded the computer's CPU, GPU, hard disk and other related information.
Traditional way of creating objects
// Computerclass Computer { private String mBroad; private String mCPU; private String mGPU; public Computer(String broad, String CPU, String GPU) { mBroad = broad; mCPU = CPU; mGPU = GPU; } @Override public String toString() { return "Computer{" + ", mBroad='" + mBroad + ''' + ", mCPU='" + mCPU + ''' + ", mGPU='" + mGPU + ''' + '}'; } }
Create a Computer object at this time like this:
Computer computer = new Computer("MSI B550M","INTEL I5","NV 3060TI");
As the business volume increases, customers also have more and more requirements. There are also corresponding requirements for the mouse, keyboard and system. Therefore, the Computer class has to undergo corresponding changes.
static class Computer { private String mOS; private String mBroad; private String mKeyBoard; private String mMouse; private String mCPU; private String mGPU; public Computer(String OS, String broad, String keyBoard, String mouse, String CPU, String GPU) { mOS = OS; mBroad = broad; mKeyBoard = keyBoard; mMouse = mouse; mCPU = CPU; mGPU = GPU; } // Just write a set method, otherwise the article will be too long and you won't write anything else public void setmBroad(String mBroad) { = mBroad; } @Override public String toString() { return "Computer{" + "mOS='" + mOS + ''' + ", mBroad='" + mBroad + ''' + ", mKeyBoard='" + mKeyBoard + ''' + ", mMouse='" + mMouse + ''' + ", mCPU='" + mCPU + ''' + ", mGPU='" + mGPU + ''' + '}'; } }
And the parameters that create Computer objects are getting longer and longer:
Computer computer = new Computer("MAC OS","MSI B550M","IQUNIX F97" ,"Logitech MX MASTER3","INTEL I5","NV 3060TI");
If there are new requirements parameters, power supply, chassis, heat dissipation, memory stick, hard disk... it's simply unimaginable.
Object initialization parameter problem
At this time, we are facing a common problem in programming. There are too many parameters required in the object, and all of them areConstructormiddletransfer, the constructor will be the same as in the example, too long, and it will be even more terrifying if it is passed using the set method.
At this time a pattern came into being, he isBuilder Mode。
Builder mode handling
/** * @author: TianLong * @date: 2022/10/17 19:58 * @detail: Product category */ class Computer{ private String mOS; private String mBroad; private String mKeyBoard; private String mMouse; private String mCPU; private String mGPU; private Computer(String OS, String broad, String keyBoard, String mouse, String CPU, String GPU) { mOS = OS; mBroad = broad; mKeyBoard = keyBoard; mMouse = mouse; mCPU = CPU; mGPU = GPU; } public static ComputerBuilder createBuilder(){ return new ComputerBuilder(); } @Override public String toString() { return "Computer{" + "mOS='" + mOS + ''' + ", mBroad='" + mBroad + ''' + ", mKeyBoard='" + mKeyBoard + ''' + ", mMouse='" + mMouse + ''' + ", mCPU='" + mCPU + ''' + ", mGPU='" + mGPU + ''' + '}'; } /** * @author: TianLong * @date: 2022/10/17 19:58 * @detail: Product Builders */ public static class ComputerBuilder{ private String mOS = "Windows"; private String mBroad= "MSI B550M"; private String mKeyBoard= "none"; private String mMouse= "none"; private String mCPU= "Intel I5"; private String mGPU= "AMD 6600XT"; public ComputerBuilder setOS(String OS) { mOS = OS; return this; } public ComputerBuilder setBroad(String broad) { mBroad = broad; return this; } public ComputerBuilder setKeyBoard(String keyBoard) { mKeyBoard = keyBoard; return this; } public ComputerBuilder setMouse(String mouse) { mMouse = mouse; return this; } public ComputerBuilder setCPU(String CPU) { mCPU = CPU; return this; } public ComputerBuilder setGPU(String GPU) { mGPU = GPU; return this; } public Computer build(){ // You can do some verification and other work in the build method if (("Gigabyte")){ throw new RuntimeException("Gigabyte is insulting China and does not support Gigabyte motherboard"); } Computer computer = new Computer(mOS,mBroad,mKeyBoard,mMouse,mCPU,mGPU); return computer; } }
Older versions and Builder versions create objects
// Old version of Computer object creationComputer computer = new Computer("MAC OS","MSI B550M","IQUNIX F97" ,"Logitech MX MASTER3","INTEL I5","NV 3060TI"); // Builder version of Computer object creationComputer computer =() .setCPU("AMD 5600X") .setGPU("NV 3060TI") .setMouse("Logitech MX MASTER3") .setKeyBoard("IQUNIX F97") .build();
The advantages can be reflected by comparing the two versions. Old versionConstructorIn-houseparameterToo many and too long, parameters of the same type are easily misaligned. When passing parameters frequently, you should also see what parameters should be passed.
The creation of the Builder mode object is simple and clear, easier to understand, and the streaming call is more beautiful and there will be no errors.
As can be seen from the code, the constructor of the Computer class is private, ensuring that all objects must be created from the ComputerBuilder class. In addition, in the build method of ComputerBuilder class, verification or other operations can be performed.
At the same time, whether there is a Set method in the Computer class is determined by your actual application scenario. Anyway, there is no need to modify the application scenario.
Things to note
- The above code is a common way of writing, not a fixed template. As long as it can passBuilder classCreating a target object can be consideredBuilder Mode。
- The constructor of the target object in the builder pattern must bePrivate modification. Otherwise, you can create the object directly. The Builder class has no meaning
- Builder ModeWhether the target object in the set method is required,Decision on specific needs. Generally, there is no Set method, which can avoid modifying the parameters in the object.
- BuilderbuildMethods, can handle someLogical issues, such as verification information, etc.
- The factory model focuses onObjects of the same typeThrough parametersControl which object to create. Builder mode focuses onsingleIn the objectParameter pass。
The above is the detailed content of saving the obsessive-compulsive disorder Android Builder mode. For more information about the Android Builder mode, please follow my other related articles!