Tutorials to .com

Tutorials to .com » Dotnet » Oop » To: design patterns Singleton (the example is C #)

To: design patterns Singleton (the example is C #)

Print View , by: iSee ,Total views: 27 ,Word Count: 1536 ,Date: Wed, 22 Apr 2009 Time: 1:50 AM

The Singleton Design Pattern
-------------------------------------------------- ------------------------------

I believe that the majority had read the introduction to "Gang Of Four" (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) classic "Design Pattern" colleagues who have admired this book will certainly be increased. It has been so declared: "Only read the" Design Pattern ", I truly level programming has been a qualitative leap."

So how do we enter the halls of design patterns? Design pattern is a veteran programmer a set of summed up a long-term reusable, object-oriented software design solutions, from this sense, the existence of the world's countless number of design patterns, "Gang Of Four" summed up the 23 kinds of design Patterns is just one of the 23 best. The key is to start understanding the "design mode" of thinking, and then they are together, flexible to apply to their own development process.

-------------------------------------------------- ------------------------------

Singleton pattern
Singleton can be said to be "Design Pattern" in the simplest and most practical model of a design. What then is the Singleton?

As the name suggests, Singleton is to ensure that a class is only an example only. Singleton object used to create, which means that, if a type of use of Singleton pattern, it is created in this category, it will have only one instance and for the visit. Very often we will need to Singleton pattern, the most common example, we hope that the whole application to connect only one instance of the database Connection; also request an application for example, only the existence of a user of the only examples of data structures. We can achieve through the application of the purpose of Singleton pattern.

A look, Singleton seems to some like a global object. In practice, however, the overall situation can not replace the Singleton object model, this is because: First, a large number of objects will make use of the overall quality of the procedure, and some programming languages such as c #, do not support global variables. Second, the method of global object and can not stop people to instantiate a class on many occasions: In addition to the overall category of examples, the developers can still create a class type constructor of a number of local examples. The Singleton pattern is a fundamental control through the creation of categories, will "ensure that there is only one example of" the task to the category itself, developers can not afford to have other ways to get multiple instances of categories. This is a global object model methods and the fundamental difference between Singleton.

-------------------------------------------------- ------------------------------

The realization of Singleton pattern
Singleton model based on two main points:

1) does not directly use the constructor type and the other to provide a Public static methods to construct the kind of example. This method is usually named Instance. Public to ensure that its overall visibility, static methods do not guarantee the creation of a redundant example.

2) Set type constructor Private, forthcoming constructor "hide", and any attempt to use the constructor method to create an example would be error. This will prevent the developer to bypass the above Instance methods to create class instances directly.

Through the above-mentioned two types of full control on the creation of: no matter how many places need to use this class, they visit are the only category that generated examples. The following C++ # code shows two ways to achieve the Singleton pattern, developers can choose one according to preference.

One way to achieve: 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!");
)
)
)

Equivalent with another approach is to achieve: 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!");
)
)
)

Compiler implementation:

Csc Singleton.cs

The results obtained:

see, only one instance!

-------------------------------------------------- ------------------------------

. NET in the Singleton
Singleton model because the value of this utility, developers can code in addition to directly use the Singleton pattern, in many large-scale systems are the realization of its shadow everywhere. Microsoft launched a grand. NET framework, the same can also be found in Singleton thinking of flashing light.

For example, in the. NET framework Remoting an important part, the remote object (Remote Object) activation in two ways: server-side and client activation mode activated. The use of server-side way to activate the object is divided into two types: Singleton object and object SingleCall. Singleton is the object of this: no matter how many of the targets of the client call, it is always only one instance, from this example to handle all client requests. On the contrary, if the remote object declaration SingleCall, the system for each method call the client to create a new object, even if they are from the same method call a client, namely, the object method calls only the duration of the existence of Once the end of method call, the object will be destroyed. It is obvious that here the object of the Singleton design pattern is Singleton thinking. NET applications.

In that case, how. NET's Remoting use Singleton? . NET provides two ways to be registered as a remote object Singleton: direct call RegisterWellKnownServiceType methods specified in the parameter object of type Singleton; or in the web.config configuration file to set the type of remote object for the Singleton. The effect of these two methods the same, the difference is that the latter method is more convenient, because the configuration file to change the content, do not have to recompile the application. The following code shows how to use the method of registration RegisterWellKnownServiceType remote object type:

RemotingConfiguration.RegisterWellKnownServiceType (Type.GetType ( "RemotingSamples.HelloServer, object"), "SayHello", WellKnownObjectMode.Singleton);

Parameters "SayHello" the client access to remote objects (in this case HelloServer) when used to represent the remote object's URI, for example, tcp: / / localhost: 8085/SayHello (assuming that the TCP channel).

Finally, a parameter for specifying the remote object is a Singleton type. Once the remote object will be registered as a Singleton, the first time in the client's method call HelloServer to create the remote object, and then maintain it until the client disconnects or object overtime was destroyed. During this period, no matter how many client calls the distant object, all the customer requests that already exist will be the only examples of acceptable deal.

This is the Singleton in. NET applications.

Singleton pattern from the realization and application also can be seen that good design patterns tend to have "the beauty of simplicity." They used a kind of "elegant" approach to successful design and architecture can be simply and easily reused. This is why software development is now a growing emphasis on "design patterns" the reason why. If you want to learn more about design patterns, or recommend you read Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides classic "Design Pattern".

-------------------------------------------------- ------------------------------

Author: Liu Dan


.Net Object Oriented Programming Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.