Introduction
ManyProgramDesign involves more or less design patterns. About23Design mode, which is classified as follows:
I] creational patterns(Build Mode)
- Singleton (Singleton mode)
- factory (factory mode)
- Abstract Factory (Abstract Factory)
- builder (Creation Mode)
- prototype (prototype)
Ii] Structural Patterns(Structured Mode)
-
- Adapter(Adapter Mode)
-
- Bridge(Bridge Mode)
-
- Composite(Compound mode)
- Decorator(Decorative Mode)
-
- Facade(Facade Mode)
-
- Flyweight(Metadata Mode)
-
- Proxy(Proxy Mode)
III] behavioral pattern(Behavior mode)
-
- Chain of responsibility(Responsibility chain model)
-
- Command(Command mode)
-
- Interpreter(Interpreter Mode)
- Iterator(Iterator Mode)
-
- Mediator(Intermediary Mode)
-
- Memento(Memorandum Mode)
-
- Observer(Observer Mode)
-
- State(Status Mode)
- Strategy(Policy Mode)
-
- Template Method(Template method)
-
- Visitor(Visitor Mode)
This articleArticleThe implementation of the singleton mode is discussed.
Concept
The most significant advantages of the design pattern are as follows:
◆ They provide you with a solution to the existing similar problems that have passed the project test. This solution promotes the development of complex relationship modules towards minimal coupling. They isolate possible changes in the system, making it easier to understand and maintain the entire system.
◆ The design mode is more effective for communication between designers. Software professionals can immediately draw a high-level design scheme in their minds, and think of the name of the design pattern that previously addressed similar problems.
Singleton mode:In Singleton mode, the active Singleton class has only one instance. All single-instance class instantiation results in the same instance. This mode also provides a global interface to access the instance of this class.
Class Diagram
Sequence Chart
Implementation
Follow these steps to implement the singleton mode:
Step 1:
CreateConsole ApplicationProject namedSingletontest.
Step 2:
AddSingletonAnd addCode:
/// <Summary>
///Summary Description for Singleton.
/// </Summary>
Public Class Singleton
{
// Fields
Private Static Singleton instance;
/// <Summary>
///Standard default constructor
/// </Summary>
Protected Singleton () {}
/// <Summary>
///Static Method for creating the single instance
///Of the constructor
/// </Summary>
/// <Returns> </returns>
Public Static Singleton instance ()
{< br> // initialize if not already done
If (instance = null )
instance = New Singleton ();
// return the initialized instance of the singleton class
return instance;
}
}
Step 3:
Create a client class to access this Singleton class as follows:
/// <Summary>
///Summary Description for client.
/// </Summary>
Class Client
{
/// <Summary>
///The main entry point for the application.
/// </Summary>
[Stathread]
Static Void Main ( String [] ARGs)
{< br> // constructor is protected -- cannot use new
Singleton S1 = Singleton. instance ();
Singleton S2 = Singleton. instance ();
If (S1 = S2)
console. writeline ( " the same instance " );
console. readline ();
}
}
Step 4:
Run this program to get the following results:
Conclusion
In this way, we use. Net framworkAn example of Singleton mode is provided. Like all theories, the single-profit model also has its advantages and disadvantages.
Advantages of Singleton mode:
1, Instance control:The Singleton mode prevents other objects from instantiating themselves and ensures that all objects access one instance.
2, Scalability:Because the class itself controls the instantiation process, the class has the corresponding scalability to change the instantiation process.
Disadvantages of Singleton mode:
1System overhead.Although the system overhead looks small, check whether the instance exists every time you reference this class instance. This problem can be solved through static instances.
2, Development obfuscation.When using an object in the singleton mode (especially defined in the class library), developers must remember that they cannot useNewKeyword to instantiate the object. Because developers cannot seeSource codeSo they are surprised when they find that a class cannot be instantiated.
3, Object lifecycle.In Singleton mode, no object destruction is proposed. In the development language that provides memory management (for example, based on. NetframeworkOnly the single-instance mode object can destroy the object instance, because it has reference to the instance. In various development languages, suchC ++Other classes can destroy the object instance, but doing so will cause the pointer inside the singleton class to be unknown.