1. Single-piece mode (Singleton pattern)

Source: Internet
Author: User

Create Pattern---Single-piece mode (Singleton pattern)
Motive (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 ensure that a class creates only one instance?
This should be the responsibility of the Class Designer, not the user of the class.
Structure diagram:

Intention:
Ensure that a class has only one instance and provides a global access point to access it.
------<< Design Mode >>gof
Examples of life:

Applicability:
(1) When a class can have only one instance and the customer can access it from a well-known access point.
(2) When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.

Code implementation:
(1) Single thread singleton implementation

Class Singlethread_singleton
{
private static Singlethread_singleton instance = NULL;
Private Singlethread_singleton () {}
public static Singlethread_singleton Instance
{
Get
{
if (instance = = null)
{
Instance = new Singlethread_singleton ();
}
return instance;
}
}
}


The above code does not have any problems with single-threaded scenarios. But in the case of multithreading is not safe.
If two threads run simultaneously to if (instance = = NULL) to determine whether the instance is instantiated, a thread evaluates to true after creating the
Instance = new Singlethread_singleton (); Before another thread also determines (instance = = null), the result is also true.
This violates the principle of the singleton pattern (to ensure that a class has only one instance).
How to implement singleton in multithreaded situations?
(2) Multithreading Singleton implementation:

1 class Multithread_singleton
2 {
3 private static volatile Multithread_singleton instance = null;
4 private static Object Lockhelper = new Object ();
5 Private Multithread_singleton () {}
6 public static Multithread_singleton Instance
7 {
8 Get
9 {
if (instance = = null)
11 {
Lock (Lockhelper)
13 {
if (instance = = null)
15 {
Instance = new Multithread_singleton ();
17}
18}
19}
return instance;
21}
22}
23

This program is secure for multithreading and uses a helper object, Lockhelper, to ensure that only one thread creates an instance (if instance is empty, only one thread is guaranteed instance = new Multithread_singleton (); Create a unique instance). (Double Check)
Note that a keyword volatile, if the keyword is removed, it is still possible that the thread is not secure.
Volatile ensures that the strict multithreaded compiler does not fine-tune instructions when the code is compiled.
(3) Static singleton implementation


3 Class Static_singleton
4 {
5 public static readonly Static_singleton instance = new Static_singleton ();
6 private Static_singleton () {}
7}


The above code expansion is equivalent to 1 class Static_singleton
2 {
3 public static readonly Static_singleton instance;
4 Static Static_singleton ()
5 {
6 instance = new Static_singleton ();
7}
8 Private Static_singleton () {}
9}

It can be seen that the principle of singleton is fully complied with.
Advantages: Simple, easy to understand
Disadvantage: the creation of an instance with parameters cannot be implemented.
(Note: The above code and information is borrowed from the Li Jianzhong Teacher's MSDN and Terrylee articles.) )

1. Single-piece mode (Singleton pattern)

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.