[. Net edge slice series] 1-singleton mode (I am not what you think),. net edge slice
Edges are rarely used when you program them, or you don't know anything at all. I call them edges. This is called. net corner material. In fact, this series is a pure C # corner Material Series.
Why. net edge, because. net coder is getting fewer and fewer, so there are fewer and fewer people who know things in the corner. Although the value is not great, we need to hold the idea of throwing bricks and jade and keep more people. net. net core is booming-I improved other languages later. This is how we pull a person into the water. The spirit of getting two people into the water supports me. I hope I can finish writing the. net edge Material Series. I am not a code producer, but a code Porter, so if there is an open source code address for the code that appears in the corner, I will certainly attach the open source code location for the launch users to indulge in it, unable to extricate themselves.
- Single-case mode of edge Materials
If the singleton mode is an alternative, it is estimated that I will be sprayed. Basically, I may be asked during each interview, and I do not know the singleton mode. Do you mean to be a. net programmer? Therefore, we are not talking about the traditional Singleton mode, nor about thread security, as well as lazy loading and inert loading.
However, do the singleton mode conform to the six principles of the design mode? At least one responsibility principle is not met. It must ensure its own singleton and its original meaning. In the design mode, nothing can be split. If not, split it again.
So next we will officially introduce our edge materials --Generic Singleton
Nothing is more convincing than the source code. First, go to the source code address:NopCommerce(Https://github.com/nopSolutions/nopCommerce) This is the famous nopCommerce, it has more cattle, Baidu, anyway, I do not know. Class file: Singleton. cs
1 public class Singleton<T> : Singleton 2 { 3 static T instance; 4 public static T Instance 5 { 6 get { return instance; } 7 set 8 { 9 instance = value;10 AllSingletons[typeof(T)] = value;11 }12 }13 }14 15 public class SingletonList<T> : Singleton<IList<T>>16 {17 static SingletonList()18 {19 Singleton<IList<T>>.Instance = new List<T>();20 }21 public new static IList<T> Instance22 {23 get { return Singleton<IList<T>>.Instance; }24 }25 }26 27 public class SingletonDictionary<TKey, TValue> : Singleton<IDictionary<TKey, TValue>>28 {29 static SingletonDictionary()30 {31 Singleton<Dictionary<TKey, TValue>>.Instance = new Dictionary<TKey, TValue>();32 }33 34 public new static IDictionary<TKey, TValue> Instance35 {36 get { return Singleton<Dictionary<TKey, TValue>>.Instance; }37 }38 }39 40 41 public class Singleton42 {43 static Singleton()44 {45 allSingletons = new Dictionary<Type, object>();46 }47 48 static readonly IDictionary<Type, object> allSingletons;49 50 public static IDictionary<Type, object> AllSingletons51 {52 get { return allSingletons; }53 }54 }
The Singleton mode is the simplest design mode, and the generic Singleton mode is also simple to horrible. What do you mean by code analysis? But I 'd like to say a few more words to make it happen.
1. There are four classes: Singleton, Singleton <T>, SingletonList <T>, and SingletonDictionary <TKey, TValue>. The inheritance relationship is Singleton <T> integrated from Singleton, while SingletonList <T>, SingletonDictionary <TKey, TValue> inherited from Singleton <T>.
If you are not familiar with generics, I can tell you that generic classes can inherit from non-generic classes and non-generic classes can inherit from generic classes, as long as you want nothing to do.
2. For these four classes, all content is static-constructors, fields, and attributes. For a static constructor, this is different from a common constructor in three aspects: first, it is only used to initialize static variables, second, the static constructor is not called by other classes (so public and private constructor cannot be modified). It is only enough to be called during clr initialization. Finally, the static constructor cannot have a parameter list, because no one can pass parameters to it.
3. for generics, List <int> and List <string> are completely different classes, so although SingletonList <T> only has one static variable, however, all types that are not generalized (after the specific type is substituted) have their own static variables. Therefore, each T type corresponds to the Instance attribute of SingletonList <T>, therefore, generic classes ensure that each instance is globally unique-that is, the singleton mode.
4. For a Singleton non-generic class, the unique function of Singleton is to provide a set for retrieval of other objects and which types have been cached by the generic Singleton class. This is completely unnecessary. But he reminds us of the following:For non-generic classes, all static variables of each class must be placed in the parent class of the generic class. In other words, it is necessary to exercise caution when there are static variables in a generic class, and whether or not to put them in the parent class.
Although the source code of the generic Singleton is too large, it does not necessarily meet our requirements. Therefore, we sometimes have practical requirements. Below we will provide the simplified generic Singleton source code.
public class Singleton<T> where T:new() { static T instance=new T(); public static T Instance { get { return instance; } set { instance = value; } } }
Although the wildcard Singleton is indeed a side sliceGeneric,Static Constructor, AndNop1_cE is definitely not a side slice. May these seemingly irrelevant edges be helpful to you.