24 design modes-Singleton pattern]

Source: Internet
Author: User

This mode is very interesting and simple, but I still want to say that it is so widely used and so popular. Singleton means a single, standalone model, so what is a single copy? Your thinking is
Just one copy. What else can't be used? Let's give an object that is hard to copy: Emperor
In the history of China, there were very few emperors in the coexistence period, but there were not many emperors. Then we thought the emperors were a singleton model. In this scenario, there were emperors and ministers, the minister needs to go up to the Emperor every day. The emperor who visits the emperor today applies the same application as yesterday and the day before yesterday (not considering the transitional period). After the Minister finishes his head, he looks up and looks up. Hi, the Emperor yesterday, the singleton mode, and the absolute Singleton mode.
First look at the class diagram:
  

Then, let's look at the program implementation and determine an Emperor first:

1 package COM. iadmob. singleton; 2 3/** 4 * @ author http://www.cnblogs.com/initial-road/ 5 * The History of China is generally an emperor of a dynasty. If there are two emperors, it is necessary to PK an Emperor out 6 */7 public class Emperor {8 // define an emperor to put it there, and then give the Emperor the name 9 Private Static Emperor = NULL; 10 11 private Emperor () {12 // you are subject to secular and moral constraints, with the aim of not letting you generate the second emperor 13} 14 15 public static Emperor getinstance () {16 if (Emperor = NULL) {17 Emperor = new emperor (); 18} 19 Return emperor; 20} 21 22 // What is the name of the Emperor? 23 public static void emperorinfo () {24 system. out. println ("I am an Emperor .... "); 25} 26} 27 28 then define Minister: 29 package COM. iadmob. singleton; 30 31/** 32 * @ author http://www.cnblogs.com/initial-road/33 * Minister is every day to see the Emperor, today to see the Emperor and yesterday, the day before yesterday is not the same that there is a problem! 34 */35 @ suppresswarnings ("all") 36 Public Class Minister {37 38 public static void main (string [] ARGs) {39 // 40 Emperor emperor1 = Emperor on the first day. getinstance (); 41 emperor1.emperorinfo (); // What is the name of the Emperor on the first day? 42 43 // The next day 44 Emperor emperor2 = emperor. getinstance (); 45 emperor2.emperorinfo (); 46 47 // The third day 48 Emperor emperor3 = emperor. getinstance (); 49 emperor3.emperorinfo (); 50 51 // the emperor who sees the same person in three days is a pleasure! 52} 53 54}

No, the minister sees the same emperor every day, and there will be no confusion. It is the case that an emperor is good or bad. As long as the emperor is mentioned, everyone knows who it is, clear and clear.
The problem is that this is the general situation. There is another example. Just as there are two Emperors in the same dynasty in a period, what should we do?

The Singleton mode is very simple, that is, adding a constructor to the constructor. The access permission is private. This mode is simple, but risk and risk are revealed in a simple way? What are the risks? In
In the B/S Project, a thread is created after each HTTP request is sent to the J2EE container, and each thread must create the same singleton object. What should I do? Okay, let's write a general Singleton program, and then
Analysis:

1 package COM. iadmob. singleton; 2 3/*** 4 * @ author http://www.cnblogs.com/initial-road/ 5 * universal Singleton mode 6 */7 public class singletonpattern {8 private singletonpattern = NULL; 9 10 // restricted cannot generate an instance 11 private singletonpattern () {12 13} 14 15 public singletonpattern getinstance () {16 if (this. singletonpattern = NULL) {17 this. singletonpattern = new singletonpattern (); 18} 19 return this. singletonpattern; 20} 21 22}

Let's look at the part of the IF condition. If there are two threads a and B, thread a will execute this. Singleton, pattern = new singletonpattern (); applying for memory allocation, you may need
0.001 is subtle. In this 0.001 is subtle, thread B executes to If (this. singletonpattern = NULL). Do you say whether the condition is true or false at this time? Is true, then?
Thread B also goes down, so there will be two Singleton instances in the memory, to see if there is a problem?

What if you use the serial number or create a signal resource for this Singleton? Chaotic business logic! Data Consistency Verification Failed! The most important thing is that you have no problem with the code.
Is the most terrible! Because in this case, you can't reproduce it, but it's chilling. How can we modify it? There are many solutions. I will talk about a simple and thorough solution to the problem:

1 package COM. iadmob. singleton; 2 3/** 4 * @ author http://www.cnblogs.com/initial-road/ 5 * universal Singleton mode 6 */7 public class singletonpattern {8 Private Static final singletonpattern = new singletonpattern (); 9 10 // restricted cannot generate an instance 11 private singletonpattern1 () {12} 13 14 public synchronized static singletonpattern getinstance () {15 return singletonpattern; 16} 17 18}

A new object is directly passed to the class member variable singletonpattern. When you want it, getinstance () is directly returned to you to solve the problem!

24 design modes-Singleton pattern]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.