Design Patterns Summary 5-creation 5-Singleton patterns

Source: Internet
Author: User

Purpose:
Singleton mode is used to ensure that a class has only one instance and that this unique instance has only one global access point. It ensures that the class is instance once, And all requests of this class point to this unique instance. In addition, this object is not created as needed. In Singleton mode, this constraint is guaranteed by Singleton classes, rather than the unique instance of classes implemented by clients using other methods.
Role
The purpose of the Singleton pattern is to ensure that there is only one instance of
Class, and that there is a global access point to that object. The pattern ensures that
The class is instantiated only once and that all requests are directed to that one and
Only object. Moreover, the object shoshould not be created until it is actually needed. In
The Singleton pattern, it is the class itself that is responsible for ensuring this con-
Straint, not the clients of the class.

Design:

Uniquesingletoninstance, Private Static Singleton type member
Singleton (), private constructor
Getinstance ()
Implementation:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace Singleton
{
Class Program
{
Static void main (string [] ARGs)
{
Sigcls sigobj = sigcls. getinstance ();
Console. writeline (sigobj. strdata );
Console. writeline (sigobj. gethashcode (). tostring ());

Console. Readline ();
Console. writeline (sigobj. strdata );
}
}
Public class sigcls
{
Private Static sigcls siginstance;
Private sigcls ()
{
Strdata = "sig ";
}

Public String strdata
{
Get;
Set;
}
Public static sigCls GetInstance ()
{
If (sigInstance = null)
SigInstance = new sigCls ();
Return sigInstance;
}

}
}

 

 

Use Cases:

1. Make sure that the class only has a unique instance.
2. Controlling access to this instance is critical in the system
3. This instance will be used multiple times after the class instance
4. The single instance is controlled by the instantiated class, rather than by other mechanisms.

Use the Singleton pattern when...
• You need to ensure there is only one instance of a class.
• Controlled access to that instance is essential.
• You might need more than one instance at a later stage.
• The control shoshould be localized in the instantiated class, not in some other mechanic.
Summary:

Some classes only have one instance in the system to ensure their logic correctness and good efficiency. The purpose of the single-piece mode is to ensure that a class has only one instance and provide a global access point for the instance. The Singleton class uses a private constructor. : Singleton t1 = new Singleton (). It indicates that Singleton () is inaccessible during compilation and the constructor is private. Pay attention to the following points in the single-threaded single-piece mode:
1. constructor privatization (If this type is to be inherited, you can use protected to declare the constructor)
2. do not support the IClinieable interface because multiple object instances may appear.
3. serialization is not supported.
4. in single-piece mode, only the management of object creation is taken into account, and the management of object destruction is not taken into account (create your own object and hand over the destruction to the Garbage Collector)
5. Unable to cope with multi-threaded environments, because multiple object instances may appear.
How can this problem be achieved under multiple threads? The Code is as follows:

 

Code

Class singletonmuli // multi-threaded Singleton Mode
{
Private Static volatile singletonmuli _ instance; // volatile is used to prevent the compiler from adjusting the location of the compiled code.
Private singletonmuli (){}
Private Static object lockhelper = new object (); // helper, not involved in Object Construction
Public static singletonmuli f_instance
{
Get
{
If (_ instance = null)
{
Lock (lockHelper)
{
If (_ instance = null) // double check
{
_ Instance = new SingletonMuli ();
}
}
}
Return _ instance;
}
}
}

 

 

 

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.