Design Mode-singleton Mode
What is Singleton mode?
Singleton mode ensures that a class has only one instance and provides the instance to the entire system.
Features:
A class can only have one instance. You can create this instance yourself. You must use this instance throughout the system. How to Implement the singleton mode?
The Singleton design mode has two forms:
"Hungry" and "lazy"
Let's take a look at the legendary hunger style:
Public class Single {private Single () {}// constructor privatize private static Single instance = new Single (); // instantiate object public static Single getInstance () {return instance; // The object returned by the exposure method }}
The feature of the hungry Chinese Style: objects are instantiated directly when an object is created.
Let's look at "lazy ":
Public class Single {private Single () {}// constructor privatize private static Single instance = null; public static Single getInstance () {if (instance = null) // check whether the instance is empty here {instance = new Single (); // The object is empty, the instantiated object} return instance; // The returned object }}
Lazy features: initialization is performed only when a method is loaded. Lazy lazy loading design mode.
Take a closer look at the disadvantages of lazy code. Let's take a look.
Class Single {private static Single s = null; private Single () {} public static Single getInstance () {// who is the lock? Bytecode file object; if (s = null) // This judgment reduces the judgment and increases the efficiency {synchronized (Single. class) // ensure thread security {if (s = null) // determine whether the object is empty s = new Single (); // instantiate object} return s; // return variable }}
When multiple threads access the lazy mode, the lazy mode performs multiple statements on the common data. Therefore, thread security problems are prone.
We have added several judgments to analyze them:
S = null is used to reduce judgment and increase efficiency synchronized (Single. class). To ensure thread security, if (s = null) is used to determine whether the object is null.
To solve the problem, add the synchronization mechanism to solve the security problem. However, the efficiency is reduced. Therefore, we do not recommend "lazy", which is simple, efficient, and safe.
Where is the singleton mode used?
Website counters are generally implemented in the singleton mode, otherwise it is difficult to synchronize. Generally, the log application of an application is implemented in singleton mode. This is generally because the shared log file is always in the open state, because only one instance can be operated; otherwise, the content cannot be appended. The configuration objects of Web applications are generally read in the singleton mode because the configuration files are shared resources. The database connection pool is generally designed in the singleton mode because database connection is a type of database resource. Database Connection pools are used in database software systems to reduce the efficiency loss caused by opening or closing database connections. This is expensive because it is maintained in the singleton mode, this can greatly reduce the loss. The multi-threaded thread pool is generally designed in the singleton mode, because the thread pool needs to conveniently control the threads in the pool. The file system of the operating system is also a specific example of the implementation of the large Singleton mode. An operating system can only have one file system.
Now, our Singleton design model is coming to an end, and it is not very difficult to interpret Singleton, the purpose of the design pattern is to facilitate programming so that it can be used. Therefore, the design pattern has mastered its nature, and there will be no such shadow of "high, let's continue to cheer up!