</pre> Singleton mode is a common design pattern, generally there are three ways, the singleton mode has the following characteristics: <p></p><p><span style= "White-space:pre" > 1. In the properties of the class, initialize a static private class instance <span style= "White-space:pre" ></span>2. The way to write your own construction method in private Reject other classes or actions use the constructor again to instantiate the Singleton class again with the <span style= "White-space:pre" ></span>3. Provide an open method to the outside world to obtain its own unique single instance </span ></p><p><span style= "White-space:pre" ></span> in a nutshell: The Singleton pattern class has only one instance of its own initialization, To provide the example to the outside world by means of the method </p><p> what is the use of singleton mode, or what are the common patterns of singleton? </p><p><span style= "White-space:pre" ></span> you for example, the printer manager in our computer, there is only one manager in the computer, But our computer can be connected to more than one printer at the same time, the role of the manager is to manage the printer work, to avoid the appearance of two printers at the same time, there will be a print conflict phenomenon, the manager is mainly to do this thing, our computer this kind of manager, memory pool, etc. are a single example of a performance, Singleton mode is designed to avoid collisions at the same time. </p><p> Common Singleton mode has three, here for the first time to introduce two, because the third I have not had time to study it! </p><p>1. A hungry man singleton mode </p><p></p><pre name= "code" class= "Java" >pubic class Demo () { private static Demo demo = new demo ();//create Private, static instance of this class private demo () {}//private privately constructed method, deny initialize second instance public demo Getdemo () {//Public open method of obtaining an instance return demo;} Other methods and actions}2. Lazy single-case mode
Pubic class Demo () {private static demo demo = Null;private Demo () {}//private Private construction method, deny initialize second instance public demo Getdemo () {// Public open Get Instance method if (demo = = null) Demo = new demo (); return demo;} Other methods and actions}
But obviously, the above notation is non-thread safe.
Tip: Thread Safety
Thread safety is my code in a number of threads running at the same time, the results of multi-threaded running out of the results of running with a single-threaded case is not consistent, if not consistent, then we can say that this is non-thread-safe, and vice versa is a thread-safe, the above code, it is clear that the non-thread-safe The same is true for a hungry man, non-thread-safe.
In other words, if a piece of code is running, the thread is said to be atomic, or to switch back and forth between different processes without causing the result to be two semantic, then it is threaded, so we don't have to think about running synchronously.
3. Registered Singleton mode
This singleton pattern needs to be supplemented
Here's an example! Single-instance mode the most common way I think it should be JDBC to get connection time, the following is written a few days in the course of my internship, see at the same time give me some of this thing, I also just started internship, these I have to continue to study hard to be able to!!
Example:
In the project, the driven environment needs to be manually set up and added, the code is as follows:
Public final class Server {/** * ConnectionFactory object */private ConnectionFactory factory = null;/** * Singleton mode */private Stati C Server SVR = new Server ();/** * Construction Method */private Server () {}/** * get singleton mode entity * * @return Server */public static server Geti Nstance () {return SVR;} /** * Get database Connection Pool Factory * * @return connectionfactory */public synchronized connectionfactory getconnfactory () {if (factory = = N ull) {try {miniproperties ppt = new Miniproperties ();pp t.load (Server.class.getResourceAsStream ("/jdbc.properties")); <span style= "White-space:pre" ></span>//Here you need to note the path, change the code details in the following section of code Factory = new Poolconnectionfactory (" Test ", PPT);} catch (Exception e) {throw new runtimeexception ("JDBC Config file read failed! ");}} return factory;} public static void Closeresource (Connection conn, Statement Stmt,resultset rs) {if (rs! = null) {try {rs.close ();} catch ( Exception e) {}}if (stmt! = null) {try {stmt.close ();} catch (Exception e) {}}IF (conn! = null) {try {conn.close ();} catch (Exception e) {}}}}