"Object-oriented"--a singleton pattern of design patterns

Source: Internet
Author: User


Single-Case mode:

definition: guarantees that a class has only one instance and provides a global access point to access it.

Usually we can have a global variable that makes an object accessible, but it doesn't prevent you from instantiating multiple objects. One of the best ways is to have the class itself responsible for preserving its only instance. This class guarantees that no other instance can be created, and it can provide a way to access the instance.


GetInstance is a static method that is primarily responsible for creating its own unique instance, and then allowing the client to access its only instance.

Multi-threaded Singleton: The solution is to access the Singleton class at the same time, which can cause problems creating multiple instances.

To solve this problem, the process can be locked, lock, to ensure that when one thread is in the critical section of the code, another thread does not enter the critical section, and if another thread attempts to enter the locked code, it waits until the object is freed.

          Class Singleton    {        private static Singleton instance;        private static readonly Object syncRoot = new Object ();        Private Singleton ()        {        } public        static Singleton getinstance ()        {            if (instance = = NULL)   // One weight: First determine whether the instance exists, and then decide whether to lock            {                lock (syncRoot)                {                    if (instance = = NULL)//double: because lock, only one can enter this, when one to instantiate it, No further entry is necessary. Avoid multiple instantiations at the same time.                    {                        instance = new Singleton ();            }}} return instance;        }    }

This is the double lock

In addition to solving multi-threading, there is an easier way to implement: static initialization:

The public sealed class singleton//sealed prevents derivation, and the derivation may increase the instance    {        private static readonly Singleton instance = new single ton ();//The instance is created the first time any member of the class is referenced, and the common language runtime handles the initialization of the variables.        Private Singleton () {} public        static Singleton getinstance ()        {            return instance;        }    }

Like static initialization, it instantiates itself when it is loaded, which is called: a hungry man Singleton class . And like the previous singleton pattern, to be quoted for the first time, it is called a lazy Singleton class .

A hungry man is an object that is instantiated as a class is loaded, so it consumes system resources in advance. However, the lazy-type, but also face multi-threaded access security issues. Therefore, the need for the mode, but also according to the actual situation.

In short: the singleton is to ensure that a class has only one instance and provides a global access point to access it.

"Object-oriented"--a singleton pattern of design patterns

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.