1. What is a singleton mode
Simply understood, there is a class that can have only one instantiated object, which is the singleton pattern.
2. Benefits of GetInstance
First look at how to use getinstance to implement singleton mode
Public classConnectionPool {Private StaticConnectionPool Pool; //other member variables//... PrivateConnectionPool () {DS=NewCombopooleddatasource (); } Public Static FinalConnectionPool getinstance () {Try{Pool=NewConnectionPool (); } Catch(Exception e) {e.printstacktrace (); } returnPool; } //Other methods//...}
The advantage of doing this is to save memory and prevent memory wastage. The other is to achieve the same provisioning of resources to prevent data conflicts:
In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.
In addition, abstract classes can not be instantiated, so use getinstance
Java singleton patterns and benefits of getinstance