In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "singleton pattern case analysis of Java design pattern" with detailed content, clear steps and proper handling of details. I hope that this "singleton pattern case analysis of Java design pattern" article can help you solve your doubts.
What is the singleton pattern?
Singleton pattern (Singleton Pattern) is one of the simplest design patterns in Java. This type of design pattern is a creative pattern, which provides the best way to create objects. This pattern involves a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique object directly without instantiating the object of the class.
Note:
1. A singleton class can only have one instance.
2. The singleton class must create its own unique instance.
3. The singleton class must provide this instance to all other objects.
The single case model is roughly divided into lazy Chinese type and hungry Chinese type, followed by a case study.
Lazy style one
Whether Lazy is initialized: yes
Whether multithreading is safe: no
Difficulty to achieve: easy
Description: this is the most basic implementation, and the biggest problem with this implementation is that it does not support multithreading. Because synchronized is not locked, it is not strictly a singleton pattern.
In this way, lazy loading is obvious, does not require thread safety, and does not work properly in multithreading.
It doesn't matter if you don't understand the concept, let's give an example.
Case 1:
Create a sword class that instantiates a sword.
Both Rainbow Cat and Blue Rabbit want to build a sword. Rainbow Cat first made a sword, named Changhong Jian, and then Blue Rabbit also made a sword, but did not name it.
Now let's analyze two situations, one in normal mode and the other in singleton mode.
Normal mode
Swords:
Public class Jians {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;}}
Test class:
Public class Demo1 {public static void main (String [] args) {/ / rainbow cat's sword Jians hong = new Jians (); / / Blue Rabbit's sword Jians lan = new Jians (); hong.setName (Changhong Jian); System.out.println (hong.getName ()); System.out.println (lan.getName ());}}
Results:
In normal mode, I new the swords of Rainbow Cat and Blue Rabbit. In fact, it is two swords, two different objects.
Singleton mode
Swords:
The getInstance () method in the sword class has a sword-making function, that is, the function of new a sword object.
Public class Jian {private static Jian jian; private String name; public static Jian getInstance () {if (jian = = null) {jian = new Jian ();} return jian;} public String getName () {return name;} public void setName (String name) {this.name = name;}}
Test class:
Public class Demo {public static void main (String [] args) {/ / rainbow cat's sword Jian hong = Jian.getInstance (); / / blue rabbit's sword Jian lan = Jian.getInstance (); / / rainbow cat named Changhong Jian hong.setName ("Changhong Jian"); / / output System.out.println (hong.getName ()) System.out.println (lan.getName ());}}
Results:
In singleton mode, I new the swords of the rainbow cat and the blue rabbit. In fact, it is a sword, the same object.
Why aren't threads safe?
Swords in singleton mode:
Public class Jian {private static Jian jian; private String name; public static Jian getInstance () {if (jian = = null) {jian = new Jian ();} return jian;} public String getName () {return name;} public void setName (String name) {this.name = name;}}
Take this class, for example, it is not thread-safe. Because he gets the object through the getInstance () method. If it is multithreaded, thread 1 and thread 2 have entered this method successively, because thread 1 has just entered the method and has not yet returned the object, thread 2 has entered the method. So thread 2 will also new an object, because thread 2 is still null when it enters the method.
Lazy type two
Whether Lazy is initialized: yes
Whether multithreading is safe: yes
Difficulty to achieve: easy
Description: this approach has a good lazy loading and works well in multithreading, but it is inefficient and does not require synchronization in 99% of the cases.
Advantages: initialize only the first call to avoid memory waste.
Disadvantages: synchronized must be locked to guarantee singletons, but locking will affect efficiency.
Why is thread safe?
The above case is also used here, which mainly introduces the difference between lazy type one and lazy man type two.
The main difference between lazy type one and lazy man type two lies in swords.
Lazy swordsmanship:
Public class Jian {private static Jian jian; private String name; public static Jian getInstance () {if (jian = = null) {jian = new Jian ();} return jian;} public String getName () {return name;} public void setName (String name) {this.name = name;}}
The swords of the lazy type II:
Public class Jian {private static Jian jian; private String name; public static synchronized Jian getInstance () {if (jian = = null) {jian = new Jian ();} return jian;} public String getName () {return name;} public void setName (String name) {this.name = name;}}
Add a field in front of the getInstance () method synchronized,synchronized is a lock, the effect is that only one thread can enter the method at a time, so as to avoid the situation that two threads enter the method and new the two objects, so it is thread-safe.
Hungry Han style
Whether Lazy is initialized: no
Whether multithreading is safe: yes
Difficulty to achieve: easy
Description: this method is more commonly used, but it is easy to produce junk objects.
Advantages: without locking, the execution efficiency will be improved.
Disadvantages: initialize the class when it is loaded, wasting memory.
The hungry Han style is to new an object directly in the class, that is, whether you need a sword or not, I have already built a sword.
Hungry Han style swords:
Public class Jian {private static Jian jian=new Jian (); private String name; public static synchronized Jian getInstance () {return jian;} public String getName () {return name;} public void setName (String name) {this.name = name;}}
Test class:
Public class Demo {public static void main (String [] args) {/ / rainbow cat's sword Jian hong = Jian.getInstance (); / / blue rabbit's sword Jian lan = Jian.getInstance (); / / rainbow cat named Changhong Jian hong.setName ("Changhong Jian"); / / output System.out.println (hong.getName ()) System.out.println (lan.getName ());}}
The getInstance () method of the sword class in the hungry Han style has lost the function of making a sword, and the test class calls it just to return a sword made in advance.
The difference between lazy Chinese style and hungry Chinese style
In the same case, both Rainbow Cat and Blue Rabbit want to build a sword. Lazy people have a sword class, and there is a way to make a sword in the sword class. When calling this method, make a sword. There is also a kind of sword in the hungry Han style, but the difference is that this kind of sword has made the sword directly, and there is no way to make it. Whether you want to make a sword or not, the sword has already been made.
Advantages: without locking, the execution efficiency will be improved. Disadvantages: initialize the class when it is loaded, wasting memory.
Now it is easy to understand the advantages and disadvantages of the hungry Han style.
After reading this, the article "singleton pattern instance Analysis of Java Design pattern" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.