1. Scene description
The builder pattern is the same as the factory pattern and the abstract factory pattern, and is used to create inherited class objects.
Factory mode: Java design mode factory mode
Abstract factory pattern: Java design pattern abstract factory pattern
The difference is that in the factory mode, each subclass implements interfaces and creates subclass objects through the factory class; while in the builder mode, each subclass has its builder class, and creates different parent class objects through it, and ultimately implements polymorphism. In fact, subclasses and parent classes do not exist in the code.
Taking the instrument data acquisition tool as an example, in factory mode, define the interface "Instrument Data Acquisition Tool", define the subclasses "PDF file data acquisition tool" and "Excel file data acquisition tool", and implement the interface in the subclass. Subclass objects are created through factory classes (whether it is a static factory class or an abstract factory class), and these objects have different expressions, that is, polymorphism.
In the builder mode, define the parent class "Instrument Data Acquisition Tool", and then use "Builder Class of PDF File Data Acquisition Tool" and "Builder Class of Excel File Data Acquisition Tool" to assign different expression methods (assign different values to fields, etc.), and finally achieve polymorphism.
In the builder mode, Director is like a director. It calls the builder Builder to complete the creation of class objects. The caller can just call Director, as shown in the figure below:
Of course, the factory model is literally used to create objects, while the builder model is literally used to build different objects. The two can achieve similar functions, but the application scenarios of the two can be very different.
The factory model is used to create a simple class object, while the builder model is used to build a complex class object. The specific information can be learned from Baidu.
When expanding, for example, add Word file data collection and create its Builder class, which implements the creation of Word file data collection programs; when calling, you can call it using WordBuilder class.
2. Sample code
(Parent) Class:
package lims.designpatterndemo.builderdemo;public class EquipmentDataCapture { private String filePath; private String equipmentData; // public String getFilePath(){ return this.filePath; } public void setFilePath(String filePath){ this.filePath = filePath; } public String getEquipmentData(){ return this.equipmentData; } public void setEquipmentData(String equipmentData){ this.equipmentData = equipmentData; }}Builder Interface:
package lims.designpatterndemo.builderdemo;public interface EquipmentDataCaptureBuilder { //Component void buildFilePath(); void buildEquipmentData(); //Assemble EquipmentDataCapture buildEquipmentDataCapture();}PDF data collection builder category:
package lims.designpatterndemo.builderdemo;public class PdfFileCaptureBuilder implements EquipmentDataCaptureBuilder{ // EquipmentDataCapture capture; //Construct public PdfFileCaptureBuilder(){ capture = new EquipmentDataCapture(); } // @Override public void buildFilePath() { capture.setFilePath(".pdf"); } @Override public void buildEquipmentData() { capture.setEquipmentData("pdf file content"); } @Override public EquipmentDataCapture buildEquipmentDataCapture() { return capture; }}Excel Data Acquisition Builder:
package lims.designpatterndemo.builderdemo;public class ExcelFileCaptureBuilder implements EquipmentDataCaptureBuilder{ // EquipmentDataCapture capture; //Construct public ExcelFileCaptureBuilder(){ capture = new EquipmentDataCapture(); } @Override public void buildFilePath() { capture.setFilePath(".xlsx"); } @Override public void buildEquipmentData() { capture.setEquipmentData("excel file content"); } @Override public EquipmentDataCapture buildEquipmentDataCapture() { return capture; }}Director class, can be simply understood as director class:
package lims.designpatterndemo.builderdemo;public class EquipmentDataCaptureDirector { public EquipmentDataCapture constructEqiupmentDataCapture(EquipmentDataCaptureBuilder builder){ builder.buildFilePath(); builder.buildEquipmentData(); EquipmentDataCapture capture = builder.buildEquipmentDataCapture(); return capture; }}Calling side example:
package lims.designpatterndemo.builderdemo;public class BuilderDemo { public static void main(String[] args) { EquipmentDataCaptureDirector direcotr = new EquipmentDataCaptureDirector(); EquipmentDataCaptureBuilder builder = new PdfFileCaptureBuilder(); builder = new ExcelFileCaptureBuilder(); EquipmentDataCapture capture = direcotr.constructEqiupmentDataCapture(builder); System.out.println(capture.getEquipmentData()); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.