From the C # Singleton design mode implementation to see the importance of the developer's development .NET Framework feature
Recently, I am learning how to read some information in the design mode in the C # language, where the Singleton design pattern has attracted my attention.
Developers who have learned design patterns know the Singleton mode. I want to explain that this design pattern is for friends who have not learned the design model. Singleton design mode is to tell you how to create a global object of a unique class example in your application, that is, this object can only be instantiated once, this object also provides a global access point access to it. Such objects such as window managers in the application or printed leave, database connection pool, etc.
Now let's take a look at the C implementation of the Singleton mode in the design pattern (for convenience, I write the implementation code into inline):
Class Singleton
{
PUBLIC:
Static singleleton * instance ()
{
IF (_Instance == 0)
{
_INSTANCE = New Singleton;
}
Return_INSTANCE;
}
protected:
Singleton ();
Private:
Static Singleton * _INSTANCE;
}
Stington * Singleton :: _ instance = 0;
Here I don't want to explain the above code, you can refer to the references provided later in this article.
In order to better understand C #, I also showed the Singleton mode implementation code in Java (not someone thinking C # is the plagiarism of Java J):
Class Singleton
{
Public Singleton Instance ()
{
IF (_instace == null)
{
Synchronized (Class.Forname ("Singleton"))
{
IF (_Instance == null)
{
_INSTANCE = New Singleton ();
}
}
}
Return_INSTANCE;
}
protected singleton () {}
Private static singleleton _instance = null;
}
Double detection mechanisms are used in the Java code described above to avoid Singleton instantiation of multiple threads. Similarly, I don't want to explain any of the above code.
First, let's take a look at the implementation of Java in C #:
Class Singleton
{
Public Static Singleton Instance ()
{
IF (_Instance == null)
{
Lock (TypeOf (Singleton))
{
IF (_instace == null)
{
_INSTANCE = New Singleton ();
}
}
}
Return_INSTANCE;
}
protected singleton () {}
Private static volatile singleton _instance = null;
}
Now, when we open your eyes, the following is the code that uses the .NET Framework platform advantage to realize the Singleton mode:
Sealed Class Singleton
{
private kindleton ();
Public Static Readonly Singleton Instance = New Singleton ();
}
Very surprised, not only the code is reduced, but also solves the performance of thread problems. There is a problem that the compiler optimization is avoided. Can the above code really work properly? If you can, how do it work? Note that the Singleton class is declared as Sealed to ensure that it will not be inherited, followed by the instance method, turn the original _instance member variable into public readonly, and is initialized during declaration. Through these changes, we really got the Singleton mode, because during the JIT processing, if the Static property in the class is used in any method, .NET Framework will initialize this property, so that the initialization instance property is initialized Singleton class instance is created and loaded. The private constructor and readonly (read only) guarantees that Singleton will not be instantiated again, which is the intention of the Singleton design mode.
This article is not to discuss design patterns, but just want to explain one by this example, it is proficient in .NET Framework and flexibly use it for the importance of practical application development.
references:
- Design mode: can be used for object-oriented software base [GOF]
--Design Pattern In C # [Jame W Cooper]
--Exploring The Singleton Design Pattern