Java singleton and lazy
Java Singleton Mode
Hungry Chinese Singleton
We can understand the hunger mode as follows: This Singleton class is very hungry and needs to be eaten urgently, so it creates an object immediately when the class is loaded.
Lazy Singleton type
We can understand the lazy model as follows: This Singleton class is very lazy and can only act as needed. We never know how to be prepared as soon as possible. It determines whether an existing object is needed. If not, it creates an object immediately and returns the result. If the existing object is no longer created, it returns immediately.
The Singleton design mode is usually used for JDBC-connected databases.
Note:
1 we often use the first Hunger style, because:
(1) Since the singleton design mode is used to use the singleton class object, it is first instantiated.
(2) There are some security risks in the lazy style. You need to add the synchronization keyword synchronized. Otherwise, we won't be able to talk about Singleton, but synchronized is less efficient.
2 In the first method, the Code private static final Single SINGLE = new Single (); why is there final?
Because classes modified by final keywords cannot be inherited, member variables modified by final cannot be modified.
Final is used here to strengthen and emphasize the concept of "the same object"-only such an object, and it cannot be modified.
If you do not need to use final to modify Single SINGLE, there will be a situation where the business is very complex and this object is modified inadvertently, resulting in various errors.
First (hunger ):
Ideas:
(1) do not allow other classes to create objects of this class. The constructor will be set to private soon!
(2) define a Class Object
(3) provide the custom object, that is, define a get method, and return the object of this class.
Analysis:
Step 1: privatize the constructors in the singleton class and use a get () function to provide such objects, so other classes cannot construct such objects.
Step 2: If other classes want to call the methods in this Singleton class, they only need to use the internal name. Method Name (). This requires the method to be static.
Step 3: Because the static method can only access static members! Therefore, you must set SINGLE to static
Public class SingleDemo {public static void main (String [] args) {Single s1 = Single. getSingle (); s1.setNumber (44); Single s2 = Single. getSingle (); s2.setNumber (55); System. out. println ("s1.number =" + s1.number); System. out. println ("s2.number =" + s2.number); if (s1.equals (s2) {System. out. println ("s1 and s2 are the same object: s1 = s2"); // if conditions are true }}} class Single {int number; public int getNumber () {return number;} public void setNumber (int number) {this. number = number;} private Single () {}; // Step 1 private static final Single SINGLE = new Single (); // step 3 public static Single getSingle () {// Step 1 and Step 2 return SINGLE ;}}
Second (lazy ):
Package cn.com; public class SingleDemo2 {public static void main (String [] args) {Single s1 = Single. getSingle (); s1.setNumber (44); Single s2 = Single. getSingle (); s2.setNumber (55); System. out. println ("s1.number =" + s1.number); System. out. println ("s2.number =" + s2.number); if (s1.equals (s2) {System. out. println ("s1 and s2 are the same object: s1 = s2"); // if conditions are true }}} class Single {int number; public int getNumber () {return Number;} public void setNumber (int number) {this. number = number;} private Single () {}; private static Single SINGLE = null; adding synchronized to multiple threads is critical !!! If (SINGLE = null) {SINGLE = new Single (); return SINGLE ;}else {return SINGLE ;}}}
The above is a detailed explanation of the java Singleton design mode. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!