Java design pattern-Singleton pattern

Source: Internet
Author: User

In some scenarios, you need to find an object to take responsibility for, and this object is the unique instance of its class. In this case, the singleton mode can be used. The purpose of Singleton mode is to ensure that a class has only one instance and provide a global access point for it. There are many ways to create an object with unique roles. However, no matter how you create a singleton object, you must ensure that other developers cannot create a new instance of the singleton object. When designing a singleton class, you need to determine when to instantiate the singleton object of the class. One way is to create an instance of this class and use it as the static member variable of this class. For example, a class may include this line: 1 private static Factory factory = new Factory (); this class obtains the unique instance of this class through a public getFactory () static method. If you do not want to create a singleton instance in advance, you can delay initialization when you first need the instance. 1 public static Factory getFactory () {2 if (factory = null) 3 factory = new Factory (); 4 //..... 5 return factory; 6} If You Want To initialize a singleton model in a multi-threaded environment, you must be careful not to initialize the singleton object simultaneously by multiple threads. In multiple thread environments, it cannot be ensured that when other threads start to execute this method, the current thread has completed the complete execution of this method. This may occur when two threads initialize a singleton object at the same time. Assume that the first thread finds that the singleton object is null, and the second thread finds that the singleton object is null. Then, both threads initialize the object. To avoid this competition, we need to use the lock mechanism to coordinate the execution of different threads on the same method. Java supports multi-threaded development. It can provide a lock for each object to indicate whether the object is occupied by the thread. To ensure that only one thread initializes the singleton object, you can lock the appropriate object to synchronize initialization. Other methods that require mutual access to the singleton object can also be synchronized through the same lock mechanism. Copy code 1 import java. util. * 2 3 public class Factory {4 private static Factory factory; 5 private static Object classLock = Factory. class; 6 7 private Factory () {8 //... 9} 10 11 public static Factory getFactory () {12 synchronized (classLock) {13 if (factory = null) 14 factory = new Factory (); 15 return factory; 16} 17} 18 19} copy the code getFactory () method to ensure that when one thread starts to initialize a singleton instance, if the other thread starts the same operation, then the second thread will wait until it obtains The lock of the ssLock object. When it acquires the lock, it will find that the Singleton is no longer null. The object is unique and does not mean the singleton mode is used. The Singleton mode provides a unique entry for object creation by hiding the constructor, so that the class responsibilities are concentrated in the singleton of the class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.