Build mode-singleton Mode

Source: Internet
Author: User

Introduction

ManyProgramDesign involves more or less design patterns. About23Design mode, which is classified as follows:

I] creational patterns(Build Mode)

    1. Singleton (Singleton mode)
    2. factory (factory mode)
    3. Abstract Factory (Abstract Factory)
    4. builder (Creation Mode)
    5. prototype (prototype)

Ii] Structural Patterns(Structured Mode)

  1. Adapter(Adapter Mode)
  2. Bridge(Bridge Mode)
  3. Composite(Compound mode)
  4. Decorator(Decorative Mode)
  5. Facade(Facade Mode)
  6. Flyweight(Metadata Mode)
  7. Proxy(Proxy Mode)

III] behavioral pattern(Behavior mode)

  1. Chain of responsibility(Responsibility chain model)
  2. Command(Command mode)
  3. Interpreter(Interpreter Mode)
  4. Iterator(Iterator Mode)
  5. Mediator(Intermediary Mode)
  6. Memento(Memorandum Mode)
  7. Observer(Observer Mode)
  8. State(Status Mode)
  9. Strategy(Policy Mode)
  10. Template Method(Template method)
  11. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.