Applying the Singleton Design Pattern to Real-World Scenarios

There are many who believe that the Singleton pattern is an anti-pattern. By the title you probably assumed I am not one of those people, and you will be right. I believe the term "anti-pattern" is being used for cases where a pattern is not being used for what it was originally designed for or just implemented incorrectly. I am going to try to keep this short. But I need to explain what the Singleton pattern is before I provide a real-life example of its applicability. The Singleton pattern is one of the original Gang of Four Design Patterns. The Singleton pattern falls under the Creational Pattern category. This means that the purpose of the Singleton pattern is to create objects. The creational purpose of the pattern is to ensure that one and only one object is instantiated in the life of the application. By definition, an anti-pattern is a pattern that is usually ineffective and highly counterproductive. So, why do classify Singleton as an anti-pattern? I found an interesting article by Michael Safyan where he expresses his opinion on the subject. However, if you examine his article closely, his conclusion are more based on the misuse of the pattern rather than in inherent qualities with the pattern itself. In my opinion, if a tool is not used for its intended purpose, that doesn't make the tool bad. But, I digress. Perhaps I should write another article about real anti-patterns. The Singleton Pattern's purpose is to ensure that one and only one object is created during an application's life cycle. Now that we understand its purpose, we must understand how and when to apply it. The "when" will address the use-cases for this pattern. For this blog, I will provide just o. The "how" will address the implementation techniques to create a Singleton.

When to use Singleton

Contrary to what some people believe, there are many valid cases for using Singletons. One of such cases is to create states on a Finite-State-Machine (FSM). In software, FSMs are implemented following the State Design Pattern. I will expand on this topic by writing about Real-Life applicability of the State pattern. For now, I simply wish to focus on using Singleton pattern to create a State object. Consider the daily cycle of a human being. In a single day, human beings engage in activities we could call "states". In the most basic sense, in a single day, humans are either asleep or awake. If you were to model this behavior in software, how would you model it? Ask yourself this simple question: Is the "asleep state" of a given person today different from the "asleep state" of the same person tomorrow? If you think about it, the state itself is the same. So, wouldn't it make sense to model this state as a Singleton? The answer is yes. In fact, when you examine the State design pattern, it is recommended that the Singleton Pattern is used to create state objects.

How to Create a Singleton

In Java, the easiest (and recommended) way to implement a Singleton is by using the Java
enum.

public enum MyEnum
{
  INSTANCE;

  private MyEnum()
  {
    // DO SOMETHING?
  }
}

Part II of this blog will be coming out in a few days. In that blog, I will be writing about applicability of the State Pattern. I will make use of the Singleton Pattern create the State objects in a FSM that I will use to implement a simple Wizard. Stay tuned!

Comments

Popular posts from this blog

Combining State and Singleton Patterns to Create a State-Machine

Exception Handling: File CRUD Operations Example

The Beauty of the Null Object Pattern