60: Design Patterns: Singleton.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

The singleton creational pattern is simple and often used. It’s actually used a bit too often so this episode will give you some caution and provide some ideas to modify this pattern when needed. There are really just 3 parts that make a class a singleton: Declare all constructors to be private. You may only have one constructor. The main thing is that you don’t leave any overloaded constructors accessible. Declare a public static method called instance or maybe getInstance that returns a pointer or a reference to an instance of the singleton class. This method will have access to the private constructor and will construct an instance of the singleton class when it’s first called. After that, it will just return the first instance that was created. Declare a private static data member that’s either a pointer or a reference to the singleton class. I say, or reference, because that may be the way some languages handle pointers. This is the data member that the instance method will use to store the first instance of the singleton. This pattern is so simple that it’s used a bit too much. The main problem is that it’s actually little better than a global variable. It makes the single instance available throughout your program. For a large program, this can cause problems debugging because the singleton can be accessed and potentially modified from anywhere. The singleton pattern is useful, just don’t overuse it. The podcast episode also gives you some additional tips for how you might want to modify this pattern. Listen to the full episode or you can also read the full transcript below. Transcript The basic description says that this pattern ensures that there’s only one instance of a class and that there’s global access to that single instance. First of all, why would anybody want to restrict the number of instances to just a single one? Some aspects of your program might represent services or features which really do exist just once. Maybe you only want one log file to ever be created. Or you have just a single database where you keep important information. Or you only want to ever open a single communication channel with a server computer. There will be times when you want to restrict instances like this and the singleton pattern shows you a simple way to accomplish this. Let’s take this step-by-step. In order to ensure that there’s only one instance, we need some way to control access to what code can create instances. All we need is to protect the constructors. After all, if outside code can’t call the constructors, then it can’t create instances. This means we need the constructors to be either protected or private. If the constructors are protected, then all some other code needs to do is derive from our class and it has full access to the constructors. Sure we won’t have instances of just this class everywhere, but instead, this class can be included as a base class instance in other classes. That’s not what this pattern describes. The only way then to make sure that instances can never be created is to make all the constructors private. But, wait a minute. What good is a class that can never be constructed? Remember that making a method private restricts access to that method from any code outside of the class itself. But other code in the class is allowed to call private methods. There’s another important step needed to implement a singleton and for that, we need a static method. You can listen to episode #32 which describes static methods if you need to. A static method can be called even without creating an instance of the class. It will also have access to the private constructors even if the static method is itself public. The static method is usually called instance or getInstance and it acts as a gatekeeper. You see, before it creates a new instance of the singleton class, it first checks if it’s ever been called