C # An overview of the object-oriented design model -- Singleton single piece (Creation Mode)

Source: Internet
Author: User

Singleton single piece (Creation Mode)
Motivation)
In software systems, there are often such special classes that must ensure that they only have one instance in the system to ensure their logic correctness and good efficiency.
How to bypass the conventional constructor and provide a mechanism to ensure that a class has only one instance?
This should be the responsibility of the class designer, not the responsibility of the user.

Intent)
Ensure that a class has only one instance and provides a global access point for the instance.
Structure)
 
Single-thread Singleton mode implementation

Class Singleton
{
Private static Singleton instance;

Private Singleton (){}

Public static Singleton Instance
{
Get
{
If (instance = null)
{
Instance = new Singleton ();
}
Return instance;
}
}
}

Class Test
{
Public static void main ()
{
Singleton S = singleton. instance;
}
}

Key Points of Singleton mode for a single thread
The instance constructor in singleton mode can be set to protected to allow subclass derivation.
The Singleton mode generally does not support the icloneable interface, because it may lead to multiple object instances, which is contrary to the original intention of the singleton mode.
The Singleton mode generally does not support serialization, because it may also lead to multiple object instances, which is also contrary to the original intention of the singleton mode.
In singletom mode, only object creation management is taken into account, and object destruction management is not considered. In terms of the overhead of platforms and objects that support garbage collection, we generally do not need to perform special management on their destruction.
Unable to cope with multi-threaded environments: In a multi-threaded environment, multiple instance objects of the singleton class may still be obtained using the singleton mode.

Multi-thread Singleton mode implementation

Class Singleton2
{
Private static volatile Singleton2 instance = null;

Private Static object lockhelper = new object ();

Private singleton2 (){}

Public static singleton2 instance
{
Get
{
If (instance = null)
{
Lock (lockhelper)
{
If (instance = null)
{
Instance = new Singleton2 ();
}
}
}
Return instance;
}
}
}

Use the. NET Type initialization mechanism to implement the multi-threaded Singleton Mode

Class Singleton3
{
Public static readonly Singleton3 Instance = new Singleton3 ();

Private Singleton3 (){}
}
Class Singleton3_1
{
Public static readonly Singleton3_1 Instance = new Singleton3_1 ();

Private Singleton3_1 (){}

Static Singleton3_1 ()
{
Instance = new singleton3_1 ();
}
}

Singleton mode Extension
Extend an instance to N instances, such as the implementation of the Object pool.
Transfers the call of the new constructor to other classes. For example, in a multi-class collaborative working environment, only one instance of a certain class is required for a local environment.
The core of understanding and extending the singleton mode is "how to control the arbitrary calls to the class constructor using new ".

Reference books
• Design Pattern: the basis for reusable object-oriented software gof
• Grady booch, Object-Oriented Analysis and Design
• Agile Software Development: Principles, models and practices Robert C. Martin
• Refactoring: improving the design of existing code Martin Fowler
• Refactoringto patterns joshuakerievsky

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.