Design patterns

27 Nov 2017

Observer pattern

Observer
Let’s say you want to build a system where there is N number of observers, who wants an update, whenever there is a change in the state of the subject. And observers can register and un-register from this service effortlessly. To do that job, there is a famous design pattern, in which, the subject can have HAS-A relationship with the list of observers, and have functions like register and notifyObserver. Now, every observer will inherit the Observer interface and then can register or unregister itself by calling the function of the subject class. And, to notify all the observers, the subject just have to call notifyObserver which will iterate through all the observer’s list, and call notifies function, which they have inherited from the observer interface.

Decorator pattern

decorator.gif
You have a coffee shop(Component), and you wanted to have a function in your coffee class which will give you the prices(Operation) of the coffee. You sell different type of coffee with various toppings. Sometimes a combination of many topping. It will be a headache for you if you create classes for each combination. Decorator pattern is here for you!! Assume you need to add a cream topping in cappuccino. Now you know that the resultant thing will be a coffee again right. First, we will create the cappuccino class(ConcreteComponent) which will inherit the coffee class(component). Now, we will add the topping(ConreteDecoratorB) by making an object of it and pass the cappuccino class object in the constructor. As decorator has a HAS-A relationship with the Component. Now when you call price(operation) function, it will first call operation function of base class plus topping price(addedBehavior).

Factory pattern

factory.gif
Now, Your coffee business is running great as you were using excellent decorator pattern and now you decide to open a pizza shop. Here the requirement is bit different, as here you need to bake a different type of pizza and then you need to perform various operations on it like cutting, packing and delivering to address. Indeed, you don’t want to do all this by just one class, as we all follow the god rule for classes. Here, we assign the task of pizza creation to a factory method, which will create your pizza(Product) based on the specification. And then we can operate it in the creator class. As, It suggests that our high-level components should not depend on our low-level elements; instead, they should both depend on abstractions.

Singleton Pattern:

Singleton_pattern
Every project use database right! We make so many calls, and we keep on updating the database. We don’t want that our database runs into concurrency issues. So we decide that the database class should have only one object. Now, how can you restrict someone from creating an object of a class? What if you make its constructor private ;). You heard me! We will make the class constructor private so that no one can create an object of this class except itself. We will create an object of this class and make that private too. And have a public static function to get the instance. Whoever needed, will call this function to get the _loneInstance of this class. Cool right!

Command pattern:

Command.gif
The Command Pattern allows you to decouple the requester of an action from the object that acts. Let’s say your team building FIFA.And You got the most important command to implement, that is Shoot(execute). Now your controller doesn’t need to know that whether you are operating Messi or Ronaldo. When the user press shoot, we got to shoot with player’s(state) precision and speed. Here, all player will inherit the player class(Command). Once shoot(execute) command comes, we will shoot by player’s skill set(receiver.action).

The Adapter pattern:

Adapter
Let’s say your PM gives you a weird task. In your project, you have a bird class(target) and a turkey class(Adaptee). Now, we all know how turkey flies. Your PM is asking to perform all the task on the turkey class that you were performing on bird class. Here comes the need for an adapter, it will inherit the target interface. So this adapter class(Flying turkey) will be a bird as it is inheriting the target interface. But still, it will be a turkey. Now, when someone calls the fly function(Request) of this bird object, we will call flap(Specificrequest) function five times of turkey class to give an effect of flying.