Design patterns Ep.5 - Creational - Singleton
Orestis Meikopoulos
Head of Engineering | Cultivating Technical Leadership | C# & .NET Content Creator | Public Speaker
In the previous articles we explored details on the following patterns:
In today's article, we are going to examine the last?creational?design pattern, the?Singleton?pattern.
Problem
To understand the Singleton Design Pattern, we are going to look at the two problems it is trying to solve.
First of all, it is vital for some classes to have one and only one instance (Problem #1). For example, there should be only one file system and one window manager. But why would we want to control how many instances a class may have? The answer is to be able to control access to some shared resource (e.g. a database or a file).
The idea of how a single instance is working, is to imagine that we created an instance of an object. If we then decided to create another one, instead of receiving a brand new one, we will just get the one we initially created. The above behavior is impossible to implement with a regular constructor since a constructor call must always return a new object.
The second problem the Singleton pattern is trying to solve is the need to provide a global access point to that instance (Problem #2) from the outside world. When we use global variables to store some essential objects may seem quite handy at first, but on the same time they are unsafe, since any code can potentially overwrite the contents of those variables and cause problems to our application.
The exact same way as with a global variable, implementing and using the Singleton pattern lets you access some object from anywhere in the program - the single instance we create and use throughout the lifetime of our application. On the same time, it protects that instance from being overwritten by other code.
Solution
But how do we ensure that a class has only one instance and that the instance is easily and globally accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects. A better solution is to make the class itself responsible for ensuring that no other instance can be created. This can be done by intercepting requests to create new objects and instead return a previously created instance. It can also provide a global point to access the instance from the outside world. This is the Singleton Pattern.
The implementation of the Singleton pattern mainly consists of the following two steps:
Let's examine some details of the pattern and see some code in C#.
Structure
领英推荐
Participants
As you can see in the above example, the Singleton class (Database) declares the static method GetInstance that returns the same instance of its own class. The Singleton’s constructor is entirely hidden from the client code and calling the GetInstance method is the only way of getting the Singleton object from the outside world.
The output of the above program would be:
Applicability
Use the Singleton pattern when:
1) There must be exactly one instance of a class and it must be accessible to clients from a well-known access point.
2) When the sole instance should be extensible by subclassing and clients should be able to use an extended instance without modifying their code.
You can find the source code for the above example?here.
With Singleton pattern we have come to the end of the creational design patterns. Let's sum up their definitions.
That's all for today. Cheers!