What is a singleton mode ?
Ensure that a class has only one instance and provides a global access point to access it.
Function: throughout the life cycle, all objects of a class are pointing to the same piece of memory space, and the system does not need to allocate space on the heap for each object of the class. At any time, there is only one instance of the Singleton class.
The singleton pattern is implemented by a singleton class, where the composition of the Singleton class is divided into three parts:
1. Specify that its construction method is private
2. Define a common class variable as the unique object of the class
3. Expose a public method to obtain the only instance of this class
Note: The construction method of a class is generally defined as public, but it can also be defined as private, and when a class's construction method is defined as private, we cannot use this constructor method, that is, the object of the class cannot be created by this constructor, when all the constructors of a class are defined as private, Then we will not be able to create objects of this class outside of this class, and all must provide a common way for the outside world to get a class that has already been instantiated through this unique portal.
There are two types of singleton modes:
1. A hungry man , thread-safe
2. Lazy , thread -insecure
The difference between a hungry man and lazy is only the time when the unique instance of this class is created, the A hungry man is initialized before the outside world obtains this instance, while the lazy is initializing the instance when the outside world first obtains the instance, it is no longer initialized, the lazy is thread unsafe, The reason for this is that when multiple threads are starting to create this object, because the CPU is using time-slice rotation, it may return multiple references to the Singleton object, resulting in thread insecurity.
A Hungry man type:
1 classsingleinstance{2 //define an object of this class3 Private StaticSingleInstance singleinstance =Newsingleinstance ();4 5 //Construction Method Privatization: Cannot create an object from the outside of this class by using new6 Privatesingleinstance () {7 8 }9 Ten //provides an accessible method to the outside world, returning the object of the current class One Public Staticsingleinstance getinstance () { A returnsingleinstance; - } -}
Lazy Type:
1 classsingleinstance{2 //define an object of this class3 Private Staticsingleinstance singleinstance;4 5 //Construction Method Privatization: Cannot create an object from the outside of this class by using new6 Privatesingleinstance () {7 8 }9 Ten //provides an accessible method to the outside world, returning the object of the current class One Public Staticsingleinstance getinstance () { A if(singleinstance==NULL) -SingleInstance =Newsingleinstance (); - returnsingleinstance; the } -}
Example: Create two objects of a singleton class to determine whether the references to the two objects are the same.
1 PackageCom.pattern;2 3 Public classTest1 {4 Public Static voidMain (string[] args) {5 //Create two objects6SingleInstance SingleInstance1 =singleinstance.getinstance ();7SingleInstance SingleInstance2 =singleinstance.getinstance ();8 9 //Print the address of the object to which the two references pointTenSystem.out.println ("SingleInstance1:" +SingleInstance1); OneSystem.out.println ("SingleInstance2:" +singleInstance2); ASystem.out.println (singleinstance1==singleInstance2); - - } the } - classsingleinstance{ - //define an object of this class - Private StaticSingleInstance singleinstance =Newsingleinstance (); + - //Construction Method Privatization: Cannot create an object from the outside of this class by using new + Privatesingleinstance () { A at } - - //provides an accessible method to the outside world, returning the object of the current class - Public Staticsingleinstance getinstance () { - returnsingleinstance; - } in}
Source Code
Effect:
Scenario for a singleton pattern:
(Image source: http://blog.csdn.net/tanyujing/article/details/14160941)
Not finished, to be continued.
Design mode 1: Singleton mode (1)