In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize the java singleton pattern". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the java singleton pattern.
Greetings from the maid of 0x01.
In my harem, the queen deserves to be the first in the world (I can only be the second). In order to highlight the status of the queen in the harem, the maid must greet the queen every morning.
A large group of maids come to greet the queen every day, and the queen receives these little sisters every day. Because of the uniqueness of the queen, the sisters will shout "long live the queen!" Note that people are calling empress, not empress Zhang or empress Li. Say hello to a queen every day, and you won't get confused. This requires that only one object can be generated by a class in the field of design.
So how to achieve it? The object is generated through the new keyword (this is the main way), how to control this? Constructor! When creating an object using the new keyword, the corresponding constructor will be called according to the input parameters. If we set the queen's constructor to private private access, can we prohibit the external creation of objects?
The picture below is the process of the palace maid greeting the only queen.
There are only two categories: Queen for queens and Maid for maids.
1. Code listing Queen class public class Queen {private static final Queen queen = new Queen (); private Queen () {} public static Queen getInstance () {return queen;} public static void say () {System.out.println ("I am yyj, please kneel ~");}}
By defining a constructor for private access, an object can be avoided by other classes new, while Queen itself can new an object, and other classes can access this class to get the same object through getInstance.
Now that there is a queen, the maid is going to appear.
two。 Code listing: public class Maid {public static void main (String [] args) {for (int I = 1; I < 4; iSum +) {Queen queen = Queen.getInstance (); System.out.println ("Today is" + I + "good day!") ; queen.say ();}
The results of the operation of the palace maid are as follows.
Today is the first day. I am yyj. Please wait for me to kneel. Today is the 2nd day. I am yyj. Please wait for me to kneel. Today is the 3rd day. I'm yyj. Please wait for your kneeling.
The palace maid greets the queen every day and looks up every day. Yo, an old acquaintance, or the queen of yesterday, this is the singleton model!
0x02 singleton mode 1. Concept
Singleton pattern (Singleton Pattern) is a relatively simple pattern, which is defined as follows:
Ensure a class has only one instance, and provide a global point of access to it. (make sure that a class has only one instance, and instantiate itself and provide that instance to the entire system. )
two。 Construction mode
There are usually two ways to build the singleton pattern in the Java language:
The lazy way. A global singleton instance is built when it is first used. The hungry way. A global singleton instance is built when the class is loaded.
3. Code listing public class Singleton {private final static Singleton INSTANCE = new Singleton (); / / Private constructor suppresses private Singleton () {} / / default public constructor public static Singleton getInstance () {return INSTANCE;} 4. Code listing lazy mode public class Singleton {private static volatile Singleton INSTANCE = null; / / Private constructor suppresses / / default public constructor private Singleton () {} / / thread safe and performance promote public static Singleton getInstance () {if (INSTANCE = = null) {synchronized (Singleton.class) {/ / when more than two threads run into the first null check same time, to avoid instanced more than one time, it needs to be checked again. If (INSTANCE = = null) {INSTANCE = new Singleton ();} return INSTANCE;}} so far, I believe you have a better understanding of "how to implement the java singleton pattern". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.