Design patterns: Singleton

In the last post, we saw the basics of objected-oriented sofware. We saw a simple example about cleaning a room and how to approach it either as a process or as an interaction between things. We saw various terminologies in the world of object-oriented software, such as classes, objects, methods, attributes, inheritance and interface. If you do not remember them, I encourage you to learn those from that post.

We are moving on from those basics in this post. We will be talking about design patterns. Using design patterns, objected-oriented programming solves some common patterns of problems that occur in the world of computers. As Tech 101 always promises, you will also see equivalent examples from the real world to make things simpler to understand. We start with a commonly occurring pattern called the singleton pattern.

Analogy for singleton pattern

Let’s imagine a country named ‘My Native Land’. ‘My Native Land’ is ruled by a single person at the helm. The citizens call him the President. He is the most accountable person and other countries approach this country through him. With such responsibility, it is to be ensured that there is only ONE person with the designation called president. The president is chosen by a public election.

The entire country goes through a unified election process. After much deliberation, candidates are chosen and the citizens get to vote their favourite. The candidates too need to go through a process of qualification to be eligible. E.g. certain education degree, certain work experience, etc. At the end of the process, one person gets to sit at the helm of the country.

This also means that citizens cannot simply proclaim themselves to be the president at their own free will. Nor can they simply appoint their dog, their neighbour, their grocery store owner or their village headman as their president. The only  way to choose a President is with a nation-wide election.

Also note that there can be only ONE actively serving president at any point of time. The current president gets to remain at the helm until the next election, after which only a victory will get him re-appointed. Otherwise he will have to relinquish his seat to the newly elected candidate.

The designation named ‘president’ did not exist forever. The role was created at some point of time. Probably when My Native Land embraced democracy. There was NO serving president before that. Someone was appointed for the first time, either through policy or through an inaugural election.

So, what’s the singleton pattern?

Drawing parallels from the president example, we should now define the singleton pattern. Here are the features of a singleton pattern.

001-president

  • There is a class (blueprint) named President. Only one object from the class can be present in the system at a time (similar to the ‘one president only’ analogy). If you need a refresher about classes and instances, please check this post.
  • Only one point in the system (nation-wide election for a president) can create an object from the class. This point must ensure that only one object created at a time. If there are no objects of the class, then one is created (like the first president when democracy was embraced). Thereafter, if an object needs to be created, it should replace the old one. All references to the old object are lost (the old president stepping down for a new one).
  • Barring this single point (election for president), there should be no other way to create an object from the class (e.g. citizens informally crowning themselves or choosing their unqualified neighbour as their president).
  • Referring to the class automatically refers to the single object made out of it. For instance, if a person named Jose Capitan is the current president, then he is simply referred to as ‘The President’ or ‘Mr. President’. This saves the effort of remembering and calling every president by his/her name every time the person in charge changes.

Example of singletons in computer programming

Singletons are commonly used to access your computer’s hardware efficiently. Your phone camera is a good example. What would happen if every app were to create an object of the Camera class and try to access the camera simultaneously, not knowing that other apps also want it? It would be chaos.

Instead, your phone’s operating system dictates the creation of just one Camera object using the Camera class. This object speaks directly to the camera hardware. None of the apps are allowed to create their own Camera objects. They can only refer to the Camera object created by the operating system.

When the phone boots up, there is no Camera object. As soon as the operating system detects your camera hardware, it creates the singleton Camera object for the first time. A new Camera object is only created if \ the existing camera object crashes or if the user is given a way to switch off the camera hardware and switch it on again. The old Camera object will be deleted.

Conclusion

Singleton pattern allows you to safeguard important resources for which a single point must be responsible. It protects you from the chaos that would happen if multiple points for the same responsibility are created. Can you think of good examples where you can apply the singleton pattern? Let me know in your comments.