Introduction
I believe that most of my colleagues who have read the design pattern, a classic of "Gang of Four" (Erich Gamma, Richard Helm, Ralph Johnson, John vlissides) will be highly admired for this book. Someone once declared this: "My programming level has really been a qualitative leap only after I read design pattern. "
So how can we enter the design model Hall? The design model is seniorProgramIn this sense, there are countless design patterns in the world, the 23 design patterns summarized by "Gang of Four" are only 23 of them. The key to getting started is to understand the idea of "Design Patterns", and then integrate and flexibly apply them to the development process.
--------------------------------------------------------------------------------
Singleton Mode
Singleton is the simplest and most practical design pattern in design pattern. So what is Singleton?
As the name implies, Singleton ensures that a class has only one unique instance. Singleton is mainly used to create objects. This means that if a class adopts the singleton mode, it will have only one instance available for access after the class is created. In many cases, we always need the singleton mode. The most common example is that we want only one connection instance to connect to the database in the entire application; for example, a unique instance with only one user data structure must exist in an application. We can all achieve our goal by applying the singleton mode.
At a glance, Singleton seems to be a global object. However, the singleton mode cannot be replaced by a global object because: first, using a large number of global objects will reduce program quality, and someProgramming LanguageFor example, C # does not support global variables. Second, the global object method cannot prevent people from instantiating a class multiple times: Except for the global entity of the class, developers can still create multiple local instances of the class through the class constructor. In Singleton mode, the task "ensure only one instance" is handed over to the class itself by fundamentally controlling the creation of the class. It is impossible for developers to obtain multiple instances of the class in other ways. This is the fundamental difference between the Global Object method and the singleton mode.
--------------------------------------------------------------------------------
Implementation of Singleton Mode
The Singleton mode is implemented based on two main points:
1) do not directly use the class constructor, but also provide a public static method to construct the class instance. Generally, this method is named instance. Public ensures its global visibility, and static methods ensure that no extra instances are created.
2) set the class constructor to private, that is, to hide the constructor. Any method that attempts to create an instance using the constructor will report an error. This prevents developers from directly creating class instances by bypassing the above instance method.
You can use the above two points to completely control the creation of the class: no matter how many places need to use this class, they access the unique instance generated by the class. The following C #CodeIt shows two ways to implement the singleton mode. developers can choose either of them based on their preferences.
Implementation Method 1: Singleton. CS
Using system;
Class singletondemo
{Private Static singletondemo thesingleton = NULL;
Private singletondemo (){}
Public static singletondemo instance ()
{If (null = thesingleton)
{
Thesingleton = new singletondemo ();
}
Return thesingleton;
}
Static void main (string [] ARGs)
{Singletondemo S1 = singletondemo. instance ();
Singletondemo S2 = singletondemo. instance ();
If (s1.equals (S2 ))
{Console. writeline ("See, only one instance! ");
}
}
}
Another equivalent implementation method is Singleton. CS:
Using system;
Class singletondemo
{Private Static singletondemo thesingleton = new singletondemo ();
Private singletondemo (){}
Public static singletondemo instance ()
{Return thesingleton;
}
Static void main (string [] ARGs)
{Singletondemo S1 = singletondemo. instance ();
Singletondemo S2 = singletondemo. instance ();
If (s1.equals (S2 ))
{
Console. writeline ("See, only one instance! ");
}
}
}
Compile and execute:
CSC singleton. CS
Get the running result:
See, only one instance!
-------------------------------------------------------------
Singleton in. net
Because the singleton mode has such practical value, developers can not only directly use the singleton mode in program code, but also see its shadows everywhere in the implementation of many large systems. In the. NET Framework launched by Microsoft, we can also find that Singleton's thoughts are shining brightly.
For example, in remoting, an important part of the. NET Framework, remote objects can be activated in two ways: server-side activation and client-side activation. There are two types of objects that adopt server-side activation: singleton object and singlecall object. The singleton object is such an object: no matter how many clients call the object, it always has only one instance, which is used to process all client requests. Conversely, if a remote object is declared as singlecall, the system creates a new object for each call to the client method, even if these call methods come from the same client, that is, the object exists only during the duration of the method call. Once the method call ends, the object will be destroyed. Obviously, the singleton object here is the application of the singleton idea in the design mode in. net.
So, how to use Singleton in. Net remoting ?. NET provides two methods to register a remote object as singleton: directly call the registerwellknownservicetype method, specify the object type as Singleton In the parameter; or in the configuration file web. set the remote object type to singleton in config. The two methods have the same effect. The difference is that the latter method is more convenient, because after changing the content of the configuration file, you do not have to recompile the application. The following code shows how to use the registerwellknownservicetype method to register a remote object type:
Remotingconfiguration. registerwellknownservicetype (type. GetType ("remotingsamples. helloserver, object"), "sayhello", wellknownobjectmode. Singleton );
The parameter "sayhello" is the URI used to represent the remote object when the client accesses a remote object (helloserver), for example, TCP: // localhost: 8085/sayhello (assuming the TCP channel is used ).
The last parameter indicates that the remote object is of the singleton type. Once the remote object is registered as Singleton, the remote object is created when the client calls the helloserver method for the first time, and then maintained until the client breaks the connection or the object times out and is destroyed. During this period, no matter how many clients call this distant object, all customer requests will be processed by the existing unique instance.
This is the application of Singleton in. net.
From the implementation and application of the singleton model, we can also see that excellent design patterns often have "simple beauty ". They adopt an "elegant" approach that allows for simple and convenient reuse of successful design methods and architectures. This is why software development emphasizes the "design model. If you want to learn more about the design patterns, we recommend that you read Erich Gamma, Richard Helm, Ralph Johnson, and John vlissides's classic design pattern.
--------------------------------------------------------------------------------