GoF Design Patterns – Singleton Design Pattern

Overview

The Singleton Design Pattern is a creational design pattern that restricts the instantiation of a class to a single instance. It also provides a global point of access to that instance. This pattern is useful when we must ensure that only one class instance is created and shared throughout the application. This pattern is useful in scenarios where a single instance of a resource manager or configuration manager needs to be used across the system. It is often used for driver objects, caching, thread pools, connection pools.

Different types of singleton

The Singleton Design Pattern comes in different forms. Each has its pros and cons. Below the different types of the Singleton Design Pattern are outlined, and each is described in more detail in further paragraphs.

  1. Eagerly Initialized Singleton.
  2. Singleton Initialized in a Static block.
  3. Lazy Initialized Singleton.
  4. Thread Safe Singleton.
  5. Optimized Thread Safe Singleton.
  6. Enum Singleton.
  7. Serializable Singleton.

The most misused pattern

The Singleton Design Pattern is probably the most misused design pattern. The problematic part with this pattern is that it also provides global access to an instance. This global instance and access to it is often used to hold global variables. Global variables are generally considered a bad practice in software development due to the following reasons:

  1. The lifecycle of a variable and the action flows are difficult to trace: Global variables make it hard to trace the lifecycle of the variable, and in which action flows they are used. In most cases, we have different parts of the system that reads and modifies global variables, which makes it hard to reason about action flows implemented in the system. Code is usually easy to read when it is structured in a way that we have a cause and effect, action and result. Global variables go against this kind of easy-to-understand code structure.
  2. Violation of separation of concerns: Global variables can blur the boundaries between different components or modules in the software, making it harder to enforce separation of concerns and achieve proper abstraction and encapsulation. This can lead to spaghetti code and hinder code organization and readability.
  3. Increased complexity and dependencies: Global variables can introduce unnecessary complexity and dependencies in the codebase. Changes to global variables can have unintended consequences on other parts of the code, making it harder to maintain and evolve the software over time.
  4. Reduced reusability and testability: Code that relies on global variables is often tightly coupled and difficult to reuse or test in isolation. This can harm the modularity and maintainability of the code, making it harder to refactor or extend.
  5. Risk of race conditions and concurrency issues: Global variables can be accessed and modified concurrently by multiple threads or processes, leading to race conditions and other concurrency-related issues. Managing a global state in a multi-threaded environment can be challenging and error-prone.

More on this pattern…

To read more on this and the other patterns, get “GoF Design Patterns Distilled” book from Amazon or Leanpub: