What is Singleton mode?
The Singleton mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance in a class in the system and the instance is easy to access, so as to conveniently control the number of instances and save system resources. If you want to have only one class object in the system, the singleton mode is the best solution.
Features of Singleton mode:
1. A singleton class can have only one instance.
2. You must create your own instance for the singleton class.
3. The Singleton class must provide this instance for other classes.
Where do I often use Singleton?
In computer systems, configuration files, thread pools, caches, log objects, printers, and so on are often used in the singleton mode.
The so-called "one Shan cannot be two tigers", if there is a problem easily, such as the configuration file, it is only a file after all, if there are several instances to access it and perform modification operations at the same time, at this time, a series of problems will arise.
Compared with multiple instance objects, a single instance also saves system resources.
Which of the following modes are commonly used in the singleton mode?
Generally, there are three Singleton models: lazy, hungry, and registered.
The code below will be explained directly.
① A class can create an instance because a constructor exists. If we change the access modifier of the constructor to private ), new cannot be used to create instances of this type.
② A new object exists in the singleton class. to be accessed by the outside world, we can make it static for external access (type. object ).
③ Sometimes we need to control this object. We can also keep it private and provide a getter Method for external access.
Simpletondemo1.java (Singleton class)
1 package COM. LCW. simpleton; 2 3 Public class simpletondemo1 {4 // privatize the constructor to prevent external users from directly creating the object 5 private simpletondemo1 () {6} 7 // provide static for external access 8 Private Static simpletondemo1 instance = new simpletondemo1 (); 9 10 // provide the getter Method for external access to private objects, static simpledemo1 return type 11 public static simpletondemo1 getinstance () {12 Return instance; 13} 14}
Simpletontest. Java (test class)
1 package COM. LCW. simpleton; 2 3 Public class simpletontest {4 public static void main (string [] ARGs) {5 simpletondemo1 S1 = simpletondemo1.getinstance (); 6 simpletondemo1 S2 = simpletondemo1.getinstance (); 7 8 If (S1 = S2) {// check whether the object's memory address is consistent 9 system. out. println ("S1 and S2 are the same object"); 10} else {11 system. out. println ("S1 and S2 are not the same object"); 12} 13 14} 15}
The effect is as follows:
The above is the hunger mode in singleton mode. Why is it called the hunger mode?
Because this instance is modified by static, the static modified member belongs to the class. When the class is loaded, the Member is loaded, that is, whether or not the class is called by the outside world, it has been loaded.
It looks like a hungry guy. No matter how many people are there, eat them first.
Next let's take a look at the lazy model in the order example model.
In terms of literal meaning, we have already understood the "lazy model". As the name suggests, it is different from the "Hungry" model. Since the "Hungry" model doesn't take effect for the first time, of course, the lazy model is not so diligent. It should be instantiated only when we call it.
They are similar in writing, but they do not directly create a new object when using private static to declare an object, but instantiate an object in the gette method.
Then determine whether the object is null. If it is null, an object is instantiated. If it is not null, the object is directly returned.
The code below is as follows:
Simpletondemo2.java (Singleton class)
1 package COM. LCW. simpleton; 2 3 Public class simpletondemo2 {4 // privatize the constructor to prevent external users from directly creating the object 5 private simpletondemo2 () {6} 7 // provide static for external access 8 Private Static simpletondemo2 instance = new simpletondemo2 (); 9 10 // provide the getter Method for external access to private objects, static simpledemo1 return type 11 public static simpletondemo2 getinstance () {12 Return instance; 13} 14}
Simpletontest. Java (test class)
1 package COM. LCW. simpleton; 2 3 Public class simpletontest {4 public static void main (string [] ARGs) {5 simpletondemo1 S1 = simpletondemo1.getinstance (); 6 simpletondemo1 S2 = simpletondemo1.getinstance (); 7 8 If (S1 = S2) {// check whether the object's memory address is consistent 9 system. out. println ("S1 and S2 are the same object"); 10} else {11 system. out. println ("S1 and S2 are not the same object"); 12} 13 14 15 simpletondemo2 S3 = simpletondemo2.getinstance (); 16 simpletondemo2 S4 = simpletondemo2.getinstance (); 17 18 if (S3 = S4) {// check whether the object's memory address is consistent 19 system. out. println ("S3 and S4 are the same object"); 20} else {21 system. out. println ("S3 and S4 are not the same object"); 22} 23 24} 25}
The effect is as follows:
Summarize the differences between the two modes:
1. The Hunger mode is slow when loading a class, because it still needs to instantiate an object, but it will be faster in running the call.
2. Lazy mode: It is faster to load classes. Because you do not need to instantiate objects when loading classes, but it is slower to run the call, you have to make judgments.
Another important thing is that the hunger mode is thread-safe, while the lazy mode is thread-insecure.
Singleton mode of software design patterns