1. Scene description
"Instrument Data Capture" includes two activities: collecting data and sending data to the server. You can define the "Instrument Data Capture" interface and define two methods: "Collection of Data Capture" and "Send Data sendData".
When implementing the "PDF file data collector", the "Instrument Data Collector" interface must be implemented to implement the "Data Collection" method; currently there is a "PDF file content analysis tool" class PdfFileExtractor, which implements data analysis of PDF files; therefore, the "PDF file data collector" can inherit the "PDF file content analysis tool" class and implement the "Instrument Data Collector" interface, as shown in the figure below:
The function of the adapter is to inherit the existing classes and expand its uses by implementing the interface.
The class adapter inherits the source class. Since the subclass can only inherit one parent class, the inherited source class's method to implement the target interface can be considered as the degree of adaptation.
2. Sample code
interface:
package lims.designpatterndemo.adapterclassdemo;public interface EquipmentDataCapture { public String capture(String filePath); public boolean sendData(String equipmentData);}Source class:
package lims.designpatterndemo.adapterclassdemo;public class PdfFileExtractor { public String capture(String filePath){ return "pdf file content"; }}Adapter class:
package lims.designpatterndemo.adapterclassdemo;public class PdfFileCapture extends PdfFileExtractor implements EquipmentDataCapture { @Override public boolean sendData(String equipmentData) { return false; }}Call example:
package lims.designpatterndemo.adapterclassdemo;public class ClassAdapterDemo { public static void main(String[] args) { PdfFileCapture capture = new PdfFileCapture(); String fileContent = capture.capture(""); System.out.println(fileContent); boolean rst = capture.sendData(fileContent); System.out.println(rst); }}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.