Reading Qin Xiaobo "The Zen of Design pattern"--a singleton model

Source: Internet
Author: User

The first day of New Year, here first of all wish you a happy New Year!

In this article, we introduce a very important and very common design pattern--singleton mode. For example, when we log on to QQ on the computer, the same QQ number can only be logged in once, and there is no possibility that a number can be logged in two times; again, I can only open it once, when it is running, I am not able to run two player clients at the same time. And so on. These patterns are singleton, that is, it guarantees that a class has only one instance object.

Here we use the examples in the book to explain how to construct a single case.

A country needs to have an emperor, and the emperor can only have one, then, obviously, it is a singleton. We need to construct an emperor class, and the Emperor class can have only one instance object, so there is a limitation that the constructor of this class needs to be private, otherwise wecan generate the object arbitrarily. And this class needs to have a static method(Static), and when we need the object of this class, we can use this method to get the class object.

Well, let's look at the concrete implementation:

public class __q_y_emperor {/* This is designed to ensure that it is thread-safe */private static __q_y_emperor _emperor = new __q_y_emperor ();p rivate __q_y _emperor () {}public static __q_y_emperor __q_y_getinstance () {return _emperor;} public static void __q_y_talk () {System.out.println ("I am qinyi Zhang ...");}}
The implementation of this class is the use of a singleton pattern, of course, here is only one way to implement the singleton pattern.

We need a test function to test whether this singleton mode is good, here, the scene is a few ministers to find the emperor, and then, every minister found the same emperor.

public class __q_y_emperormain {/** * @param args */@SuppressWarnings ("static-access") public static void Main (string[] Ar GS) {//TODO auto-generated method Stubint _minister = 5;for (int i = 0; I! = _minister; ++i) {/* Gets the instance object is the same */__q_y_empero R _emperor = __q_y_emperor.__q_y_getinstance (); _emperor.__q_y_talk ();}}}
We run this main function and get the following result:

I am Qinyi zhang...i am Qinyi zhang...i am Qinyi zhang...i am Qinyi zhang...i am Qinyi Zhang ...
Below, see what the class diagram is, very simple, no inheritance, and so on.


In the book, a design method called "Multiple cases Mode" is derived from the singleton model, that is, a class can produce multiple objects, but the number of objects is limited.

It is also an example of the emperor, but there are many emperors in this example, and every minister may find a different emperor.

Well, this is also easy to achieve:

public class __q_y_multiemperor {private final static int _maxnumofemperor = 3;private static arraylist<string> _emp Erorname = new arraylist<string> ();p rivate static arraylist<__q_y_multiemperor> _emperorlist = new Arraylist<> ();p rivate static int _currentnumofemperor = 0;private __q_y_multiemperor () {}private __Q_Y_ Multiemperor (String _name) {_emperorname.add (_name);}  static{/* multiple cases, the number of objects is limited */for (int i = 0; I! = _maxnumofemperor; ++i) {_emperorlist.add (New __q_y_multiemperor ("num" + (i + 1)));}} public static __q_y_multiemperor __q_y_getinstance () {Random _randrom = new Random (); _currentnumofemperor = _ Randrom.nextint (_maxnumofemperor); return _emperorlist.get (_currentnumofemperor);} public static void __q_y_talk () {System.out.println (_emperorname.get (_currentnumofemperor));}}
Test function:

public class __q_y_multimain {/** * @param args */@SuppressWarnings ("static-access") public static void main (string[] args  ) {//TODO auto-generated method Stubint _minister = 10;for (int i = 0; I! = _minister; ++i) {__q_y_multiemperor _emperor = __q_y_multiemperor.__q_y_getinstance (); _emperor.__q_y_talk ();}}}
Class Diagram:


Finally, let's run the following example:

Num 3num 2num 2num 2num 3num 1num 2num 2num 1num 1



Reading Qin Xiaobo "The Zen of Design pattern"--a singleton model

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.