Step by step. NET Design Mode Study Notes 4. Singleton (Singleton Mode)

Source: Internet
Author: User

Overview

The Singleton mode requires that a class has only one instance and provides a global access point.
This raises a question: how to bypass the conventional constructor and provide a mechanism to ensure that a class has only one instance?
When a customer program calls a class, it does not consider whether the class can only have one instance or other issues,
Therefore, this should be the responsibility of the class designer, not the responsibility of the class user.
From another perspective, the Singleton model is actually a responsibility model. Because we have created an object and this object plays a unique role, in this separate object instance, it concentrates all the power of the class to which it belongs, at the same time, it is also responsible for exercising this kind of power! Intention  Ensure that a class has only one instance and provides a global access point to it. Model Diagram  Logical Model diagram: Physical Model diagram: <Design Pattern> Singleton example Comparison:We first compare the advantages and disadvantages of the four methods: Method 1: public sealed class Singleton {private static readonly Singleton instance = new Singleton (); private Singleton () {} public static Singleton Instance {get {return instance ;}} advantages: simple and clear disadvantages: resource consumption Method 2: public sealed class ClassicSingleton {private static ClassicSingleton instance; private static object syncRoot = new Object (); private ClassicSingleton () {} public static ClassicSingleton Instance {get {if (instance = null) {lock (syncRoot) {if (instance = null ){//... custom code instance = new ClassicSingleton () ;}} return instance ;}}advantages: Resource Saving disadvantages: code lengthy method 3: public sealed class Singleton {static Singleton () {Instance = new Singleton () ;}private Singleton () {}public static Singleton Instance {get; private set ;}} advantages: Both Resource Saving and Simplicity: thread Unsafe Method 4: public class Singleton {private static Singleton instance; // Added a static mutex for synchronising use of instance. private static System. threading. mutex mutex; private Singleton () {} static Singleton () {instance = new Singleton (); mutex = new System. threading. mutex ();} public static Singleton Acquire () {mutex. waitOne (); return instance;} // Each call to Acquire () requires a call to Release () public static void Release () {mutex. releaseMutex () ;}}advantages: not only resource saving, but also simple and clear, and thread safety (one arrow, three carvings) Disadvantages: slightly lengthy The following is an example:When playing online games, calculate the number of online players. Because there are many regions, we need to use the singleton mode. first create Example. cs: html # viewSource "commandName =" viewSource "highlighterId =" highlighter_901949 "> view sourceprint?
public sealed class Example
    {
        /// <summary>
        /// Define a static Example
        /// </summary>
        private staticExample SingleExample=new Example ();
        private int SumCount = 0;
        // Private constructor
        private Example()
        
 

Related Article

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.