The Singleton pattern. Features of implementation in C#
This topic is a continuation of the topic:
Contents
- 1. The structure of the Singleton pattern. Figure
- 2. The structure of the Singleton pattern with reference to C# code. Figure with explanation
- 3. C# code that implements the Singleton pattern
- 4. Using a static property instead of a method
- Related topics
Search other websites:
1. The structure of the Singleton pattern. Figure
The structure of the Singleton pattern is shown in Figure 1.
Figure 1. The structure of Singleton pattern
⇑
2. The structure of the Singleton pattern with reference to C# code. Figure with explanation
Figure 2. The structure of the Singleton pattern. Example in C#
An instance (object) of the Singleton class is obtained by calling the Instance() method. In the Instance() method, an instance is created by calling the class constructor using the new operator. Since the constructor of the Singleton class is implemented with the protected access modifier, it will not be possible to create an instance of this class from the methods of other classes. However, you can expand the capabilities of this class.
If the task allows the possibility of extending a single class by inherited subclasses, then the constructor is declared as protected. If you want to prohibit the extension of a single class, then the constructor must be declared as private.
⇑
3. C# code that implements the Singleton pattern
using System; using static System.Console; namespace ConsoleApp9 { // Implementation of the Singleton pattern class Singleton { // A static method that returns an instance of the Singleton class. // This method can be replaced with the corresponding property. public static Singleton Instance() { if (_instance == null) { _instance = new Singleton(); return _instance; } else { return null; } } // The class constructor declared as protected, in order to: // - prohibit the creation of an instance of the class using the new operator; // - it was possible to inherit this class. protected Singleton() { } // A static internal variable that holds an instance of the class. // This variable can be accessed from the methods of this class. // There is no access to the variable from the methods of other classes. private static Singleton _instance = null; // ------------------------------------------------- // Other internal fields of the class private int d; // Property for accessing the d field public int D { get { return d; } set { d = value; } } // Method that outputs the value of the d field public void Print(string text) { WriteLine("------------------"); WriteLine("{0}. d = {1}", text, d); } } // Client class class Program { static void Main(string[] args) { // This is the client code. // Create a single instance of the Singleton class Singleton obj1 = Singleton.Instance(); // Check obj1 for null if (obj1 != null) { obj1.D = 25; obj1.Print("obj1"); } else WriteLine("obj1 == null"); // Trying to create another instance of a class Singleton obj2 = Singleton.Instance(); if (obj2 != null) { obj2.D = 77; obj2.Print("obj2"); } else WriteLine("obj2 == null"); } } }
The result of the program
------------------ obj1. d = 25 obj2 == null
⇑
4. Using a static property instead of a method
C# allows the use of properties instead of methods. Therefore, if desired, in the above code the Instance() method can be replaced with a property. Below is an example of declaring such a property
// A static property that returns an instance of the Singleton class public static Singleton Instance { get { if (_instance == null) { _instance = new Singleton(); return _instance; } else return null; } }
Using a property in a client method could be like the following
... // This is the client code. // Use the Instance property Singleton obj1 = Singleton.Instance; // Check obj1 for null if (obj1 != null) { obj1.D = 25; obj1.Print("obj1"); } else WriteLine("obj1 == null"); // Trying to create another instance of a class Singleton obj2 = Singleton.Instance; if (obj2 != null) { obj2.D = 77; obj2.Print("obj2"); } else WriteLine("obj2 == null"); ...
⇑
Related topics
- Singleton pattern. Overview. Features of use. Implementation in C++
- Using the Singleton pattern for classes that form an inheritance hierarchy. Implementation in C++, Java, C#
- The Singleton pattern. Implementation in Java
⇑