As you know, a single piece pattern is one of the design pattern > in <gof 23, and its intent is to allow only one instance of a single piece of class to exist (the extended singleton mode is not in this context) and provide a global access method. The UML class diagram is as follows.
In Http://csharpindepth.com/Articles/General/Singleton.aspx, the article enumerates 5 ways to implement a single-piece pattern, but many of the articles are more or less missing: How to ensure thread safety, How to ensure that only one instance exists, and how to expand it according to business changes.
The following is an example of C # implementation of a single piece, which explains each of these questions.
1. How to ensure thread safety
You can use static,readonly two keywords in C # to guarantee the thread safety of a single piece. Static members decorated by the. NET Framework Common Language Runtime (CLR) automatically loads the management when the program runs, and these members are sealed, global, and cannot be instantiated. Such a feature, so that the single piece of C # to achieve more convenient and concise. The ReadOnly keyword can be used to guarantee that a reference to a singleton instance is not modified under any circumstances. It also ensures the thread safety of a single piece, along with static.
2. How to ensure that only one instance exists
In C #, the Static keyword guarantees a unique reference to a single instance. The hidden constructor guarantees that the object cannot be constructed from new. This does not guarantee the uniqueness of a single instance. If we can deserialize an instance of a single piece of type, you can also clone an instance of a single piece by implementing the Clone interface.
So we want to make sure that a single piece type cannot be serialized and cannot be cloned, that is, you cannot add serializable tags and implement cloned interfaces or methods for a single piece type and extension type.
3. How to expand
In addition to the <GOF in the 23 design patterns > books that refer to the extension of a single piece type, few an article refer to inheriting extensions to a single piece of class, and even some C # implementations directly add a sealed keyword to a single piece of the class before it is sealed. But in actual projects, The variability of business requirements necessarily requires that a single piece of Class be inherited. Gof the use of a registration method to implement the inheritance of a single piece of class. In C #, we can use the inheritance class to extend a single piece class, using reflection to load different pieces of a single subtype on demand.
The following is a specific code example that shows one way to implement a single piece in C # and inherit from a single piece of class.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6
7 namespace Singletondemo
8 {
9 Class program{
The static void Main (string[] args) {
One try{
Singleton S1 = singleton.instance;
Console.WriteLine ("This is {0}.", S1. Name);
Singleton sc = s1. Clone ();
Console.WriteLine ("This is {0}.", SC. Name);
S1. Name = "small";
Console.WriteLine ("This is {0}.", S1. Name);
Console.WriteLine ("This is {0}.", SC. Name);
Mysingleton s2 = mysingleton.instance as Mysingleton;
Console.WriteLine ("{0} is {1}"), S2. Name, S2. Age);
21}
catch (Exception ex) {
Console.WriteLine ("Error:{0}", ex. message);
24}
25}
26}
The public class singleton{
public string Name = "Nadal shrimp";
//readonly can ensure thread safety
static readonly Singleton instance = Create ();
to static Singleton () {
32}
Protected Singleton () {
34}
public static Singleton instance{
get{
Panax Notoginseng return instance;
38}
39}
private static Singleton Create () {
try{
42//From external (registry, configuration file ...) Import
A string typeName = "Singletondemo.mysingleton";
The Type t = Type.GetType (typeName);
ConstructorInfo ci = t.getconstructor (New type[]{});
(Singleton) Ci. Invoke (NULL);
47}
catch (Exception ex) {
Throw ex;
50}
51}
52/*//cannot implement cloning method
Singleton Clone () {
The return of New Singleton ();
55}*/
56}
[Serializable]
Mysingleton:singleton{public class
The ' public int ' age = 28;
60}
61}
Special Note: The example of the implementation of a single piece is a personal comparison recommended many of the implementation of the method.