In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The article begins with:
Creative mode: singleton mode
Brief introduction
Name: singleton mode
English name: Singleton Pattern
Values: I dominate my life (only allow myself to instantiate, do not want to be instantiated by other objects)
Personal introduction:
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. )
(from the Zen of Design patterns)
There are three concerns here, which are:
Only one instance instantiates itself (that is, actively instantiates) to provide the entire system with the story you want.
Let's think big and explain it with a story.
Xiaoming has a car at home. I don't know what brand it is, and I don't care. Anyway, it's the only car in his family. Xiaoming is lazy and can drive whenever he goes out, such as traveling, going to school and going to parties. Let's simulate the scene when Xiao Ming goes out.
Class Car {public void run () {System.out.println ("go...") ;} class XiaoMing {public Car travel () {System.out.println; Car car = new Car (); car.run (); return car;} public Car goToSchool () {System.out.println ("Xiaoming goes to school"); Car car = new Car (); car.run (); return car } public Car getTogether () {System.out.println ("Xiao Ming attends the party"); Car car = new Car (); car.run (); return car;}} public class SingletonErrorTest {public static void main (String [] args) {XiaoMing xiaoMing = new XiaoMing (); Car car1 = xiaoMing.travel (); Car car2 = xiaoMing.goToSchool () Car car3 = xiaoMing.getTogether ();}}
There is only one way for the car above, and that is to go. Xiao Ming goes to travel, go to school and go to parties in his only car. Does anyone have any questions? Why does every method return a Car object? In fact, I just want to do an inspection below to see if Xiaoming goes to travel, school and party on the same bus. Here is the check code:
System.out.println ("car1 = = car2?" + (car1 = = car2); System.out.println ("car2 = = car3?" + (car2 = = car3))
What's the end result? It's obviously two false. Xiaoming has different cars to travel, school and parties. Doesn't Xiaoming have only one car? The key is Car car = new Car (); this code, which actually creates a car, recreates one each time. So how do you realize that Xiao Ming has only one car? At this time, the singleton mode was introduced.
Above we mentioned three points that the singleton model needs to have: there is only one instance, obviously, the above code has not only one instance, but three Car instances; self-instantiation, Car itself does not take the initiative to instantiate, but only when Xiao Ming needs to instantiate; provide this instance to the entire system, because Car is not actively instantiated, so it can not provide itself to external exposure.
Our code does not meet the requirements of the singleton pattern at all. We need to modify it to match the three main points of the singleton model. The first thing you need to implement is point 2, which changes the instantiation of Car from Xiaoming to Car itself, as follows
Class Car1 {private static Car1 car1 = new Car1 (); private Car1 () {} public void run () {System.out.println ("go...") ;}}
The above code uses private to decorate the constructor so that Car1 cannot be instantiated by other users, using Car1 car1 = new Car1 (); to actively instantiate itself.
Next, point 3 is implemented, exposing this instance to the entire system, that is, exposing itself. Each user calls the Car1.getInstance () method to get the instance.
Class Car1 {private static Car1 car1 = new Car1 (); public static Car1 getInstance () {return car1;} private Car1 () {} public void run () {System.out.println ("go.") ;}}
The above code implements the 2 and 3 points of the singleton pattern, how to achieve the first point? I'm telling you, you don't have to implement it, as long as you meet points 2 and 3, and the first point is a good idea to test whether it's a singleton pattern. Let's check it out.
Class Car1 {private static Car1 car1 = new Car1 (); public static Car1 getInstance () {return car1;} private Car1 () {} public void run () {System.out.println ("go.") ;} class XiaoMing1 {public Car1 travel () {System.out.println; Car1 car = Car1.getInstance (); car.run (); return car;} public Car1 goToSchool () {System.out.println ("Xiaoming goes to school"); Car1 car = Car1.getInstance (); car.run (); return car } public Car1 getTogether () {System.out.println ("Xiao Ming attends the party"); Car1 car = Car1.getInstance (); car.run (); return car;}} public class SingletonRightHungryTest {public static void main (String [] args) {XiaoMing1 xiaoMing1 = new XiaoMing1 (); Car1 car1 = xiaoMing1.travel (); Car1 car2 = xiaoMing1.goToSchool () Car1 car3 = xiaoMing1.getTogether (); System.out.println ("car1 = = car2?" + (car1 = = car2); System.out.println ("car2 = = car3?" + (car2 = = car3));}}
What is the result of the last two lines of the above code? That's what we want: 2 true. It shows that Xiaoming has been driving the same car these times. This is the simplest way to implement the singleton pattern, which is often called the hungry Han singleton pattern. Why do you have such a weird name? In fact, it has something to do with the corresponding lazy singleton pattern, which is the difference between the two implementations. The hungry Chinese singleton pattern creates an object when the class is loaded into memory, while the lazy one creates an object only when it is used for the first time, that is, delaying the time to create an object from loading to using it for the first time.
Let's take a look at how to implement the lazy singleton model. First describe the scene: Xiao Ming doesn't have a car yet, and he doesn't know when to buy a car. Suddenly one day, he wants to travel and thinks it's time to buy a car. Then he buys a car to travel, drives to school and goes to parties when he comes back from the trip.
Class Car2 {private static Car2 car2; public static synchronized Car2 getInstance () {if (null = = car2) {System.out.println ("Buy a car...") ; car2 = new Car2 ();} return car2;} private Car2 () {} public void run () {System.out.println ("go...") ;} class XiaoMing2 {public Car2 travel () {System.out.println; Car2 car = Car2.getInstance (); car.run (); return car;} public Car2 goToSchool () {System.out.println ("Xiaoming goes to school"); Car2 car = Car2.getInstance (); car.run (); return car } public Car2 getTogether () {System.out.println ("Xiao Ming attends the party"); Car2 car = Car2.getInstance (); car.run (); return car;}} public class SingletonRightLazyTest {public static void main (String [] args) {XiaoMing2 xiaoMing2 = new XiaoMing2 (); Car2 car1 = xiaoMing2.travel (); Car2 car2 = xiaoMing2.goToSchool () Car2 car3 = xiaoMing2.getTogether (); System.out.println ("car1 = = car2?" + (car1 = = car2)); System.out.println ("car2 = = car3?" + (car2 = = car3));}} Xiaoming went to travel to buy a car. Go. No, no, no. Xiao Ming, go to school. Xiao Ming goes to the party. Car1 = = car2? Truecar2 = = car3? True
The printed results are attached to it. Xiaoming only buys a car when he wants to travel. This is how the lazy singleton pattern is implemented.
Note that a key point of the lazy singleton pattern is that the getInstance () method comes with synchronized. Why is this?
First of all, you need to understand what the keyword synchronized does: it modifies the execution of method synchronization, that is, in the case of multithread concurrency, only one thread is allowed to execute the method at a point in time.
What will happen if you don't add this? In the case of multithreaded concurrency, if two threads execute to if at the same time (null = = car2), then both threads are judged to be true, and then both threads will execute car2 = new Car2 (), which is not a single example.
Summary
The singleton pattern can be said to be the simplest of the design patterns, and it is also often used in many scenarios, such as project configuration file loading, various tool classes, and so on. The most important thing for the singleton pattern is to consider multithreading concurrency, without which it is easy to cause the singleton object to be not singleton. The single case brings us the biggest benefit is to save memory.
The above two methods are the simplest and simplest implementations in the singleton pattern, and I believe they are also the most frequently used ones. On the Internet, many netizens have shared many ways to realize the singleton model, and you can also understand it. Before you understand it, you must already understand the two simplest ways to implement the text, or you will get dizzy.
I hope the article will be helpful to you, the design pattern series will be constantly updated, interested students can follow the official account LieBrother, the first time to get articles to push to read, can also communicate together, make friends.
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.