Design patterns Ep.5 - Creational - Singleton

Design patterns Ep.5 - Creational - Singleton

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:

  • Make the Singleton class constructor private, to prevent other objects from using the new operator
  • Create a static method that will eventually act as a constructor. This method, when called, internally calls the private constructor to create an object and stores it inside a static class field. All following calls to the static creation method return the same object.

Let's examine some details of the pattern and see some code in C#.

Structure

No alt text provided for this image

Participants

  • Singleton (Database): Defines an instance operation that lets outside clients access its unique instance. Instance is a class operation and in particular a static member function in C#. The Singleton is also responsible for creating its own unique instance.

No alt text provided for this image

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.

  • Client (Program): Clients access a Singleton instance solely through Singleton's instance operation.

No alt text provided for this image

The output of the above program would be:

No alt text provided for this image

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.

  • Factory Method: Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
  • Abstract Factory: Lets you produce families of related objects without specifying their concrete classes.
  • Builder: Lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
  • Prototype:?Lets you copy existing objects without making your code dependent on their classes.
  • Singleton:?Lets you ensure that a class has only one instance, while providing a global access point to this instance.

That's all for today. Cheers!

要查看或添加评论,请登录

Orestis Meikopoulos的更多文章

社区洞察

其他会员也浏览了