Design Pattern Learning: A single case model

Source: Internet
Author: User

Why only one instance:

For some classes in the system, only one instance is important, for example, there can be multiple print tasks in a system, but only one task is working; a system can have only one window manager or file system; A system can have only one timer tool or ID (ordinal) generator. You can only open one task Manager in Windows. If you do not use a mechanism to uniqueness a Window object, a number of Windows will pop up, if the contents of these windows are exactly the same, it is a duplicate object, wasting memory resources, if the contents of these windows are inconsistent, it means that in a moment the system has multiple states, and the actual discrepancy, will also bring misunderstanding to the user, Don't know which one is the real state. It is therefore sometimes important to ensure that the uniqueness of an object in a system is only one instance of a class.

Class Diagram:

From the class diagram, the GetInstance () method returns the instance instance.

How to declare a single case:

A hungry man-type single example:

public class Singleton
{
         private static Singleton Singleton = new Singleton ()//class loaded then initializes the single Instance object
                                                                                                                                   //is called the A Hungry man type, Because he's in a hurry.
         private Singleton ()
         {
         } public
         static Singleton getinstance ()
         {return
                   Singleton;
         }
}

URL Address: http://www.bianceng.cn/Programming/project/201602/49638.htm

Lazy Single Example:

Writing 1:

public class Singleton
{
         private static Singleton Singleton;
 
         Private Singleton ()
         {
         }
         //Note here synchronized ensures thread safety, meaning that only one thread at a time can access the method public
         static Synchronized Singleton getinstance ()
         {
                   if (Singleton = = null)
                   {//is initialized when Singleton is to be used, plus the judgment to avoid duplication of initialization, Guaranteed only a new one
                            singleton= new Singleton ();
                   }
                   Return singleton
         }
}

Writing 2: (compare writing 1, more efficient) called double check

public class Singleton
{
         private static Singleton instance = null;
 
         Private Singleton ()
         {
                   //dosomething
         } public
 
         static Singleton getinstance ()
         {
                   if (instance = = null)
                   {
                            synchronized (singleton.class)//You can think about why this increases efficiency
                            {
                                     if (null = = instance)
                                     {
                                               Instance= new Singleton ();
                   }} return instance
         }
}

Double Check:

1 The first condition is that if the instance is created, it does not need to be synchronized and returns directly.

2 Otherwise, we'll start synchronizing threads.

3 The second condition is that if a thread in the synchronized thread creates the object, then the other thread does not have to create it.

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.