Design patterns: Visitor pattern

Have you ever noticed? If you say “Hi” to Jay, he replies the same. However Jyoti always replies, “What’s up!”. When you get angry and say “Shut up!”, the reactions are different too. Jay is calm, but firm. His reply is, “Hey, watch your word, buddy!”. But Jyoti loses it and says, “Shut up yourself, dumbo!”

How can two persons interpret the same words differently and react so diversely? Well, chances are that nature has used visitor pattern to program their behaviours that way! Continue reading “Design patterns: Visitor pattern”

Design patterns: Object pool

What do car rentals and libraries have in common? They temporarily lend resources only as long as someone needs them and then take them back so that someone else can use them. In object-oriented programming, such a system is called an ‘object pool’.

Continue reading “Design patterns: Object pool”

Design patterns: Observer pattern

What is common between a washing machine that has just completed washing and your employee who tells you that she is done creating the annual reports? They are both using the observer design pattern.

Continue reading “Design patterns: Observer pattern”

Design patterns: Factory pattern

If your children are identical twins and you introduce them to someone, how do you help that someone distinguish between the twins?
You use the factory design pattern of course!

Let’s use this post to learn one of the most commonly used design patterns in the world of object-oriented programming. Instead of compelling you to remember the names of classes, the factory pattern returns you the appropriate class based on something you know about that class. Let’s dive in to see what I mean. Continue reading “Design patterns: Factory pattern”

Design patterns: Singleton

In object-oriented programming, design patterns solve challenges that occur in the world of software in the form of recognised patterns. We start with a commonly occurring pattern called the singleton design pattern. You will see equivalent examples from the real world to make things simpler to understand.

An 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/her the president. Other countries approach this country through him/her. The president is chosen by a public election, from among qualified candidates.

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

There can be only ONE actively serving president at one point in 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/she will have to relinquish the seat to the newly elected candidate.

The designation named president did not exist forever. The role was created at some point in 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 presidential example, we should now define the singleton pattern. Here are the features of a singleton pattern.


  • There is a class (blueprint) named President. Only one object from the class can be present in the software at a time (similar to only one president for the country analogy). If you need a refresher about classes, objects and instances, please check this post.
  • Only one place in the software (like the nation-wide election for a president) can create an object from the class. This place 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 instance made out of class. 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 and a new one created in its place.


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 try to assume that responsibility.

Further reading

3rd party or self-hosted? Part 2: Is self-hosting right for me?

In the last post, we saw the difference between using 3rd party services like GMail, Github and Slack to run services for our organisation and running the services on our own servers. We saw the advantages and disadvantages of each approach. Assuming that you are convinced about self-hosting, let’s see if it is worthwhile for you to pursue the quest.

To answer this question, let’s address what to consider by using the analogy of building your own library of books v/s being the member of a public library. Continue reading “3rd party or self-hosted? Part 2: Is self-hosting right for me?”

3rd party or self-hosted? Part 1: The basics

Let’s go swimming

You probably love swimming and would like to go for a swim everyday. To get into the daily habit of swimming, you enrol in the swimming club closest to your home. It has 4 clean pools, each with 7 swim lanes. A swimming club is super convenient. The club constructs the pools, maintains the cleanliness and ensures the safety standards. It may also provide other specialised services like a dedicated swimming coach. You don’t have to worry about the details. You just need to pay the monthly fees for your right to entry to the pool and then show up everyday.

But of late, you have been finding the club annoying and counter-productive to your habit. They have been continuously hiking the monthly fees and you feel the pinch. They have been constantly introducing new rules, such as maintaining seperate timings for males and females. Only one of the two genders is allowed at a time. They have been constantly playing with their opening and closing timings and also the timings at which different genders are allowed. You suddenly find that the swimming club’s timings and your work timings clash and that the swimming club is not open to your gender when you are free from work. Suddenly, they have declared a day off every Sunday since their staff wants it. You also hear some stories about spy cams placed in their dressing rooms and videos being uploaded, a clear security breach. The pools have been getting too crowded recently and there are people not following swim lanes. They swim across you and cut you off while you are in the middle of a lap. Continue reading “3rd party or self-hosted? Part 1: The basics”

Understanding cryptocurrency

You have probably heard about digital currency like Bitcoin in the recent years and read how it’s trading at an insanely high value when compared with standard currency. On 18th October 2017 (today), 1 bitcoin is trading at 3900 pounds, 5200 dollars, 4400 euros and a whopping 342000 Indian Rupees. To put this in perspective, a single bitcoin can purchase an 800cc hatchback car (Alto, Eon, i10) in India. Other cryptocurrencies are trading at very high values too.

What exactly is cryptocurrency? Why is it gaining popularity and how does it work? What exactly is mining? While the users of cryptocurrency try to explain the concept to the people who are not initiated, they fall short of easy examples and stick to their indecipherable jargon which can make the listener pull his/her hair out. In this post, I try to explain cryptocurrency to you in as lucid a language as possible. Continue reading “Understanding cryptocurrency”

How agile is your project: Part 2: Scrum and Kanban

In the last post, we saw what Agile methodology is and its manifesto. We saw an example of how Agile can be used by a tailor Bharat to sew a shirt for a customer Aditya. While we talked about Agile through a story, there are formal ways of planning and keeping track of projects using Agile. Two of them are Scrum and Kanban. Continue reading “How agile is your project: Part 2: Scrum and Kanban”

What you MUST know when you start your own website: Part 2

In the last post, we saw that to build your own website, you need: a domain name, a machine to host your content and a record that maps your domain name to the machine. We saw that instead of building your own server machine, we hire machines from a hosting company. We used an analogical story about a company that builds shelves of racks and leases it to whoever wants to use them. We also mentioned that hosting companies offer machines at different levels of readiness based on your requirements. These different levels of readiness are called hosting plans. In this post we will see which hosting plan suits which type of website. Continue reading “What you MUST know when you start your own website: Part 2”