1. Scene description
After collecting instrument data, the data needs to be sent to the lims system and uploaded by calling the lims system service.
Implement the Lims system service agent in the instrument data acquisition component. The agent needs to specify the service address url and authentication information (user name, password).
Therefore, creating the agent requires some resources. In addition, the agent does not require creating multiple instances. In this case, a singleton pattern can be used, so that only one service agent class instance is created.
2. Implementation examples
package lims.designpatterndemo.singletondemo;public class LimsService { private static String url; private static String username; private static String password; // private static LimsService service = null; //Private constructor private LimsService(){ url = "http://serviceurl"; username = "admin"; password = "pswd"; } public static LimsService getService(){ if(service==null){ service = new LimsService(); } return service; } // public boolean uploadEquipmentData(String equipmentData){ return true; }}Call example:
package lims.designpatterndemo.singletondemo;public class SingletonDemo { public static void main(String[] args) { boolean rst = LimsService.getService().uploadEquipmentData(""); 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.