Summary of Singleton Patterns of C Design Patterns

  • 2021-12-13 09:00:45
  • OfStack

Advantages and disadvantages

Advantages:

1. Instance control

Singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects access only one instance.

2. Flexibility

Because the class controls the instantiation process, the class can flexibly change the instantiation process.

Disadvantages:

1. Overhead

Although the number is small, it still requires 1 overhead to check for the existence of an instance of the class every time an object requests a reference. You can solve this problem by using static initialization.

2. Possible development confusion

When using singleton objects (especially those defined in class libraries), developers must remember that they cannot instantiate objects using the new keyword. Because library source code may not be accessible, application developers may accidentally find themselves unable to instantiate this class directly.

3. Object lifetime

Unable to solve the problem of deleting a single object. In a language that provides memory management (for example, a language based on. NET Framework), only a singleton class can cause an instance to be unallocated because it contains a private reference to the instance. In some languages (such as C + +), other classes can delete object instances, but this can lead to floating references in singleton classes

C#

Ensure that there is only one instance of a class and provide one global access point to access it

Key points of realization

The Singleton pattern restricts rather than improves class creation.

The instance constructor in the Singleton class can be set to Protected to allow subclass derivation.

Singleton schema 1 generally does not support the Icloneable interface, as this can result in multiple object instances, contrary to the original intention of the Singleton schema.

Singleton schema 1 generally does not support serialization, which may also lead to multiple object instances, which is also contrary to the original intention of Singleton schema.

Singleton only considers the management of object creation, but does not consider the management of destruction. In terms of the platform supporting garbage collection and the overhead of objects, we generally do not need to manage their destruction specially.

The core of understanding and extending the Singleton pattern is "how to control any user calls to the constructor of a class using new".

It is permissible and meaningful to simply modify an Singleton to have a few instances.

Advantages

Instance control: Singleton prevents other objects from instantiating their own copies of Singleton objects, ensuring that all objects access only one instance

Flexibility: Because the class controls the instantiation process, the class can modify the instantiation process more flexibly

Disadvantages

Overhead: Although the amount is small, if you check for the existence of an instance of the class every time an object requests a reference, you will still need 1 overhead. This problem can be solved by using static initialization, which has been mentioned in the above five implementations.

Possible development confusion: When using singleton objects (especially those defined in class libraries), developers must remember that they cannot instantiate objects using new keywords. Because library source code may not be accessible, application developers may accidentally find themselves unable to instantiate this class directly.

Lifetime of objects: Singleton cannot solve the problem of deleting individual objects. In languages that provide memory management (for example, languages based on. NET Framework), only the Singleton class can cause an instance to be unallocated because it contains a private reference to the instance. In some languages, such as C + +, other classes can be deleted

Object instance, but this will cause floating references in Singleton classes.

Applicability

When a class can only have 1 instance and customers can access it from a well-known access point.

When the only instance should be extensible through subclasses and the customer should be able to use an extended instance without changing the code.

Code example:

Double locking mechanism


namespace Singleton
{
public class Singleton
{
// Definition 1 Private static global variables to hold the only 1 Instances 
private static Singleton singleton;
// Definition 1 Read-only static objects 
// And this object is created when the program is running 
private static readonly object syncObject = new object();
///
///  Constructor must be private 
///  So it can't be used externally  new  To create an instance of the class 
///
private Singleton()
{}
///
///  Definition 1 Global access points 
///  Set to static method 
///  You can call the method outside the class without instantiation 
///
///
public static Singleton GetInstance()
{
// It is guaranteed that only instantiation 1 Times 
// That is, in the 1 Instantiated on second call 
// Later calls will not instantiate 
// No. 1 1 Heavy  singleton == null
if (singleton == null)
{
lock (syncObject)
{
// No. 1 2 Heavy  singleton == null
if (singleton == null)
{
singleton = new Singleton();
}
}
}
return singleton;
}
}
}

I hope this article is helpful to you


Related articles: