1. Scene description
The format of instrument data files includes Pdf, Word, Excel, etc. The data collection methods of files of different formats are different. Therefore, the instrument data acquisition interface is defined, and different data acquisition classes such as PDF and Excel are defined to implement this interface.
Through the factory class, different methods are called to obtain different instrument data collection classes, and the interface method is called.
If you do not use factory mode, you need to new different collection objects, and using factory mode hides the creation method of new.
As shown in the figure below:
2. Sample code
Instrument data acquisition interface:
package lims.designpatterndemo.factorydemo;public interface EquipmentDataCapture { public String capture(String filePath);}PDF file data collection class:
package lims.designpatterndemo.factorydemo;public class PdfFileCapture implements EquipmentDataCapture{ @Override public String capture(String filePath) { return "PDF file content"; }}Excel file data collection class:
package lims.designpatterndemo.factorydemo;public class ExcelFileCapture implements EquipmentDataCapture{ @Override public String capture(String filePath) { return "Excel File Content"; }}Factory category:
package lims.designpatterndemo.factorydemo;public class EquipmentDataCaptureFactory { public static EquipmentDataCapture getPdfFileCapture(){ return new PdfFileCapture(); } public static EquipmentDataCapture getExcelFileCapture(){ return new ExcelFileCapture(); }}Call example:
package lims.designpatterndemo.factorydemo;public class FactoryDemo { public static void main(String[] args) { EquipmentDataCapture edc = EquipmentDataCaptureFactory.getPdfFileCapture(); edc = EquipmentDataCaptureFactory.getExcelFileCapture(); String fileContent = edc.capture(""); System.out.println(fileContent); }}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.