Design patterns: Null object

After treating Adil, his doctor prescribes medicines in a complicated dosage. There are two medicinal tablets, one green and one red. The green medicine is to be taken 3 times in the coming week: Monday, Thursday and Saturday and the red one, 4 times: Monday, Wednesday, Friday and Sunday.

As Adil leaves, the doctor hands him two strips of medicine: 7 green tablets and 7 red ones. Adil is confused. He asks, “7 each? But doesn’t it call for 3 of these and 4 of those with a schedule?”. To which, the doctor replies. “How can I be sure that you won’t forget the complicated schedule? That’s why I have given you 7. Have one of each every day.” Adil is aghast. “But… isn’t that over-dosage?” “No, it’s not. 4 of the green tablets are simply mint candies. 3 of the red ones are strawberry candies. Inside the strips, the real tablets are interspersed with identical looking candies as per your dosage schedule. You don’t have to worry. Just habitually have one tablet of each colour every day. Start from the top of each strip.”

Brilliant! The doctor took a complex decision-making process away from Adil and just let him build a simple habit: one green tablet and one red tablet every day. The real tablets will fight against the illness that Adil approached the doctor with. The candies are there to simply … do nothing! In design pattern parlance, the doctor just used the Null Object pattern. Continue reading “Design patterns: Null object”

Design pattern: Flyweight

Adam is a cunning lad. He has been double-dating Elina and Carina. However he has identified himself with different names to each of them. Elina knows him as Adrian and Carina knows him as Boris. Is Adam cheating? Well, Adam begs to differ. He says life is short and he is a limited resource, only one person. Why not have fun with two girls at a time? Inside himself, he is always the same Adam. But he switches to two different personalities for the sake of Elina and Carina, by changing his external behaviour. Morally questionable, no doubt. But he is a fine specimen of the flyweight pattern. Continue reading “Design pattern: Flyweight”

Design pattern: Chain of responsibility

If you have ever visited a government office, you are probably directed from one counter to another to get tasks done. Why doesn’t the same person do everything? This is because the work is divided into small tasks and each government official is given the responsibility of only one task. Once done, that official will direct you to the next one. You are seeing the chain of responsibility design pattern in action. Continue reading “Design pattern: Chain of responsibility”

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.

001-president

  • 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.

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

Further reading

Introduction to object-oriented programming

A really common jargon that gets thrown around in the world of software is ‘OOP’. This does not refer to a mistake, as in ‘oops!’. On the contrary, OOP in software is mostly a right decision. OOP is an acronym for Object-Oriented Programming.

OOP is one of the ways to think about a software solution, design its architecture and use an appropriate programming language to write it. In this post, I will explain to you some concepts of object-oriented software. If you are a future software developer, this post will make it easy for you to understand. Even if you never intend to be a software developer, this post will make you look at software from a different perspective. Continue reading “Introduction to object-oriented programming”

Intro to Aadhar pay system

In the last two posts, we saw how to use BHIM and PayTM using a smart phone. But what if the paying customer does not have a smartphone. Has India’s rapidly advancing digital payments pioneer NPCI (National Payments Corporation of India) considered a way to include customers with no smartphones? Turns out it has. Of course, terms and conditions apply, but NPCI has made a way for non-smartphone customers to pay digitally. Enter Aadhar Pay. Continue reading “Intro to Aadhar pay system”