Template Method
Template method defines the skeleton of how a certain algorithm could be performed, but defers the implementation of those steps to the children classes. Real world example Suppose we are getting some house built. The steps for building might look like Prepare the base of house Build the walls Add roof A…
Strategy
Strategy pattern allows you to switch the algorithm or strategy based upon the situation. Real world example Consider the example of sorting, we implemented bubble sort but the data started to grow and bubble sort started getting very slow. In order to tackle this we implemented Quick sort. But now a…
Visitor
Visitor pattern lets you add further operations to objects without having to modify them. Real world example Consider someone visiting Dubai. They just need a way (i.e. visa) to enter Dubai. After arrival, they can come and visit any place in Dubai on their own without having to ask for permission or…
Observer
Defines a dependency between objects so that whenever an object changes its state, all its dependents are notified. Real world example A good example would be the job seekers where they subscribe to some job posting site and they are notified whenever there is a matching job opportunity.
Memento
Memento pattern is about capturing and storing the current state of an object in a manner that it can be restored later on in a smooth manner. Real world example Take the example of calculator (i.e. originator), where whenever you perform some calculation the last calculation is saved in memory (i.e.…
Mediator
Mediator pattern adds a third party object (called mediator) to control the interaction between two objects (called colleagues). It helps reduce the coupling between the classes communicating with each other. Because now they don't need to have the knowledge of each other's implementation. R…
Iterator
It presents a way to access the elements of an object without exposing the underlying presentation. Real world example An old radio set will be a good example of iterator, where user could start at some channel and then use next or previous buttons to go through the respective channels. Or take an ex…
Command
Allows you to encapsulate actions in objects. The key idea behind this pattern is to provide the means to decouple client from receiver. Real world example A generic example would be you ordering food at a restaurant. You (i.e. Client ) ask the waiter (i.e. Invoker ) to bring some food (i.e. Command ) a…
Chain of Responsibility
It helps building a chain of objects. Request enters from one end and keeps going from object to object till it finds the suitable handler. Real world example For example, you have three payment methods ( A , B and C ) setup in your account; each having a different amount in it. A has 100 USD, B has 300…
Proxy
Using the proxy pattern, a class represents the functionality of another class. Real world example Have you ever used an access card to go through a door? There are multiple options to open that door i.e. it can be opened either using access card or by pressing a button that bypasses the security. Th…
Flyweight
It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects. Real world example Did you ever have fresh tea from some stall? They often make more than one cup that you demanded and save the rest for any other customer so to save the resources e.g.…
Facade
Facade pattern provides a simplified interface to a complex subsystem. Real world example How do you turn on the computer? "Hit the power button" you say! That is what you believe because you are using a simple interface that computer provides on the outside, internally it has to do a lot …