C # Object-oriented design mode discussion on--2.singleton single piece (Create Mode)

Source: Internet
Author: User

One: Pattern classification

From an objective point of view:

Create-type (creational) mode: Responsible for object creation.

Structured (Structural) mode: Handles the combination of classes and objects.

Behavioral (behavioral) mode: The assignment of responsibilities in class-to-object interaction.

In terms of scope:

The class pattern handles static relationships between classes and subclasses.

The object pattern handles dynamic relationships between objects.

II:Singleton (create mode) Single piece

1. Motivation (motivation)

In software systems, there are often special classes that must be guaranteed to have only one instance in the system in order to ensure their logical correctness and good efficiency.

How do you bypass the regular constructors and provide a mechanism to guarantee that there is only one instance of a class?

This should be the responsibility of the Class Designer, not the user's responsibility.

2. intention (Intent)

Ensure that a class has only one instance and provides a global access point for that instance. --"Design pattern" GOF

3. structure (Structure)

Three: Realize

1. Single thread Singleton mode implementation

1.2 single thread Singleton several points:

the instance constructor in Singleton mode can be set to protected to allow subclasses to derive.

Singleton mode generally does not support the icloneable interface, as this can lead to multiple instance objects, which are contrary to the original intent of Singleton mode.

Singleton mode generally does not support serialization, as this may result in multiple instance objects, which are contrary to the original intent of the Singleton mode.

Singleton mode only takes into account the management of object creation, does not consider the management of object destruction, in terms of the cost of supporting platforms and objects for garbage collection, we do not need to manage special management of their Destruction.

It is not possible to handle multi-threaded situations, and multiple object instances may still appear in multi-threaded environments.

public class mysigleton_singleprocess    {        private static mysigleton_singleprocess instance;        Private mysigleton_singleprocess () {} public        static mysigleton_singleprocess getmysingletoninstance ()        {             if (instance==null) {                instance = new Mysigleton_singleprocess ();            }            return instance;        }    }

  

2. Multithreading Singleton mode implementation

     Public classmysigleton_multiprocess {Private StaticMysigleton_multiprocess instance; Private Static ReadOnly ObjectLockhelper=New Object(); Privatemysigleton_multiprocess () {} Public Staticmysigleton_multiprocess getmysingletoninstance () {if(Instance = =NULL)            {                Lock(lockhelper) {//double check to prevent code compilation when you make optimizations to adjust the code execution order                    if(Instance = =NULL) {instance=Newmysigleton_multiprocess (); }                }                           }            returninstance; }    }

3. Implementing multithreaded Singleton mode using the . NET initialization mechanism

   Public  classSingleton {Private Static ReadOnlySingleton _instance =NewSingleton (); //Explicit Static constructor to tell C # compiler//Not to mark type as BeforeFieldInit        StaticSingleton () {}/// <summary>        ///prevents a default instance of the/// <see cref= "Singleton"/>class from being created. /// </summary>        PrivateSingleton () {}/// <summary>        ///Gets the instance. /// </summary>         Public StaticSingleton Instance {Get            {                return_instance; }        }    }

4.Singleton Mode Expansion

Extend an instance to multiple instances, such as an implementation of an object pool.

Transfer calls from the New constructor to other classes, such as multiple classes in a collaborative work environment, where only one instance of a class needs to be owned by a local environment.

The core of the Singleton mode is "How to control any call that a user uses new to an instance constructor of a class."

5. Application of the Singleton pattern in the . NET Framework

The GetType () in the Object base class.

C # Object-oriented design mode discussion on--2.singleton single piece (Create Mode)

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.