The Singleton Design Pattern

Recently in CS-343, I have been introduced to the concept of design patterns. These are essentially code-writing techniques that help make code easier to describe, modify and maintain. There are a wide variety of design patterns, each with different benefits and drawbacks. My last class on Thursday ended as we began to cover the Singleton Pattern, and so I decided I would look into Singleton in advance of our activities next class about it. My research led me to Andrew Powell-Morse’s blog post “Creational Design Patterns: Singleton,” which can be found here:

This post, as you may expect, is focused on explaining the Singleton pattern to the reader. Powell-Morse accomplishes this by using real-world analogies to describe the concept of Singleton and a programming example to show how the pattern can be implemented in code. I chose to write about this blog not only because it explains Singleton well, but also because I found the programming example interesting. The purpose of the Singleton pattern is to ensure only a single instance of certain classes can exist at a time, which Powell-Morse clearly states right at the start of the blog. This is a simple enough concept to grasp, but Powell-Morse elaborates further by explaining that Singleton should be thought of in terms of uniqueness rather than singularity. He uses real-world examples of uniqueness, those being individuality in people and a unique deck of cards used for poker games, to describe situations in which Singleton can be useful. These examples, especially the deck of cards, have helped me understand that Singleton is useful in programs that only require a single instance of a class, and I could definitely see myself applying this concept in future projects to help reduce memory usage from unnecessary objects.

Since I found the deck of cards analogy especially helpful, I was pleased to discover that it was the focus of Powell-Morse’s programming example. However, the example’s complexity made it somewhat difficult to understand at first. Instead of simply demonstrating how Singleton is coded, Powell-Morse applies the concept to a program using multiple threads that separately remove card from the same deck instance. I have not written many programs myself that use multi-threading, and this lack of experience made the example confusing initially. The example is also written in C#, which is a language I am not nearly as experienced in as I am with Java. Despite my initial confusion, I eventually understood the example and grew to appreciate its complexity. The use of multi-threading in the example helped demonstrate a major drawback of Singleton and how to work around it in C#. The example not only taught me how to implement Singleton into my future coding projects using static variables, but it also showed me how to work around Singleton’s issues with multi-threading. This blog post also taught me more about a language that isn’t Java, which is always welcome.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at
Get started
%d bloggers like this: