. NET Design Patterns:
NET design Pattern Example Singleton pattern (Singleton pattern)
One: Introduction to the Singleton mode: (Brief Introduction)
Singleton mode (Singleton pattern), which guarantees that a class has only one instance, and provides a global access point to access it. Singleton mode because Singleton encapsulates its only instance, it can strictly control how a customer accesses it and when to access it. Simply said is a single mode: Only you can have a person to visit;
Ii. Problem Solving (what to Solve)
Consider using singleton mode when a class allows only one instance to be created.
Three single-Case Pattern analysis ( Analysis )
Singleton class , which defines a private variable instance; Private constructor Method Singleton () and Method getinstance ();
Private variable instance:
Singleton pattern Structure:
private static Singleton instance;
Private constructor Method Singleton (), you cannot use the New keyword to create an instance of this class.
Private Singleton ()
{
}
Method getinstance (), which is the only global access point for an instance of this class.
public static Singleton getinstance ()
{
If the instance does not exist, then new instance is returned, otherwise an existing instance
if (instance = = null)
{
Instance = new Singleton ();
}
return instance;
}
Singleton Pattern Code
1, single case mode class singleton |
public class Singleton { private static Singleton instance; <summary> Create a static read-only process helper when the program is running </summary> private static readonly Object _object = new Object (); <summary> The constructor method is private, and the foreign key cannot instantiate this class through the new class </summary> Private Singleton () { } <summary> This method is the only global access point for this class of instances (Double plus lock double-check Locking) </summary> <returns></returns> public static Singleton getinstance () { First determine if the instance exists, there is no additional lock processing if (instance = = null) { The part of the program with the lock at the same time has only one thread to enter, Lock (_object) { If the instance does not exist, then new instance is returned, otherwise an existing instance if (instance = = null) { Instance = new Singleton (); } } } return instance; } } |
2 , client-side code |
static void Main (string[] args) { Singleton Singleton2 = Singleton.getinstance (); Singleton Singleton3 = Singleton.getinstance (); if (Singleton2 ==singleton3) { Console.WriteLine ("Instance Singleton2 is the same as instance Singleton3! "); } Console.readkey (); } |
The singleton pattern can also be used without shackles, such as using the three-layer technology call class in a class:
#region get its own singleton mode: UserInfo represents the class file in the BLL layer
private static UserInfo instance;
public static UserInfo getinstance ()
{
if (instance==null)
{
Instance = new UserInfo ();
}
return instance;
}
#endregion
Called in the presentation layer:
. Bll. UserInfo BLL = BLL. Userinfo.getinstance ();
Design Patterns---singleton patterns in. Net