01 Singleton (Creational Pattern)

Source: Internet
Author: User

Ensure that a class has only one instance and provides a global access point for the instance.

1. Simple Singleton

1 // The Singleton design mode cannot cope with multithreading 2 class Singleton 3 {4 private static Singleton instance; 5 private Singleton () {} 6 public static Singleton Instance 7 {8 get 9 {10 if (Instance = null) 11 {12 instance = new Singleton (); 13} 14 return instance; 15} 16} 17}

2. Multithreading

1 // multithreading 2 class ThreadSingleton 3 {4 // it is still possible to generate multiple instances without volatile. 5 // ensure that the specific instances are not rearranged. // you can write methods or attributes. 6 private static volatile ThreadSingleton instance = null; 7 // helper, not involved in change 8 private static object lockHelper = new object (); 9 private ThreadSingleton () {} 10 public static ThreadSingleton Instance11 {12 get13 {14 if (instance = null) 15 {16 lock (lockHelper) 17 {18 if (instance = null) 19 {20 instance = new ThreadSingleton (); 21} 22} 23} 24 return instance; 25} 26} 27}

3. Simple Singleton Mode

// Another classic one // only applies to class MSDNSingleton {public static readonly MSDNSingleton Instance = new MSDNSingleton (); // private constructor private MSDNSingleton {}}

Explain the singleton mode above

1 class MSDNSingleton 2 {3 // implements inline initialization 4 public static readonly MSDNSingleton Instance; // = new MSDNSingleton (); 5 // static constructor 1. execution time: if you want to execute the statement, you must first execute the static Constructor (BeforeFieldInit) 2. if not, do not instantiate 6 // 3. under multiple threads, only one thread executes 7 static MSDNSingleton () 8 {9 Instance = new MSDNSingleton (); 10} 11 // private constructor 12 private MSDNSingleton () {} 13}

It is convenient, but it does not support parameterized Constructors (static constructors cannot pass parameters and are called by the system)

The methods for passing parameters by the constructor are the first and second methods. If the third method is to be implemented, you can design a method for isolating the constructor.

1 // support for parameterized constructors 2 class ParamSingleton 3 {4 private static ParamSingleton instance; 5 private ParamSingleton (int x, int y) 6 {7 this. x = x; 8 this. y = y; 9} 10 public static ParamSingleton GetInstance (int x, int y) 11 {12 if (instance = null) 13 {14 instance = new ParamSingleton (x, y); 15} 16 else17 {18 instance. x = x; 19 instance. y = y; 20} 21 return instance; 22} 23 int x; 24 int y; 25}
1. The static constructor does not support parameter passing. You can design a method 2 class MSDNParamSingleton 3 {4 public readonly static MSDNParamSingleton Instance = new MSDNParamSingleton () that is isolated from the constructor (); 5 private MSDNParamSingleton () {} 6 // isolate the 7 int x; 8 int y; 9 public int X10 {11 get12 {13 return this. x; 14} 15 set16 {17 this. X = x; 18} 19} 20 public int Y21 {22 get {return y;} 23 set {value = y;} 24} 25}

Singleton mode Extension

Extend an instance to n instances, such as the implementation of an 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 Singleton Pattern is "How to Control users and use new to call any class instance constructor ".

 

Singleton In the. NET Framework to expand applications

MyClass c1 = new MyClass ();
MyClass c2 = new MyClass ();
//
Type t1 = c1.GetType ();
Type t2 = c2.GetType ();

Recommended books:
Refactoring: improved the design of existing code Martin Fowler
Agile Software Development: Principles and Practices: Robert TC Martin
Object-Oriented Analysis and Design Grady booch
Design Pattern: Reusable basic Gof for Object-Oriented Software
Refactoring to Patterns: joshua Kerievsky

Singleton application in. NET Framework
Type and HttpContext correspond to and expand

 

 

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.