Design Patterns Card — 1
For Fast track Java Interview Preparation(for Quick Reference only not for beginers)
Design Patterns are logically divided into three types: 1. Creational 2. Structural 3. Behavioral
Must know Design Patterns are
Oberver, Singleton, Iterator, Decorator
Observer Design Pattern [Behavioural]
Observer name itself has a meaning object needs to be observed,
Whenever there is change happened in one Object all its dependent Objects need to be notified.
For Example, Travel Tickets prices are dependent on Petrol prices, so whenever petrol prices changed we need to update the ticket price as well.
The below code Demonstrates a simple Observer Pattern, Travel Bus ticket is dependent on Petrol Price. Its Petrol Object responsibility to notify its dependent classes.
Petrol Object is ready with its Subscribers list, but to get an update from Petrol it's TravelBus's responsibility to subscribe to Petrol Object.
Singleton Design Pattern [Creational]
The easiest Design pattern in all, but the Interviewer doesn't take it easy if you demonstrate it wrongly.
Singleton → one instance per class
first, How can u make a class to a singleton class,
but, there are a few problems with this kind of implementation
- This object can be cloned and can make a duplicate copy for It
- if we Serialize to a file and retrieve from that it will be a new Object
- Using Reflection API we can access private constructor
We can solve these problems this way,
- Clonable → override clone method and throw Exception
- Serializable →override readResolve method. readResolve does allow a class to replace/resolve the object read from the stream before it is returned to the caller.
- Reflection → use ENUM (suggesting to read about ENUM)
Iterator Design Pattern [Behavioural]
By having a custom iterator, we can omit some conditions and can maintain a clean code. Instead of writing a regular for loop, we can iterate with iterator which a method hasNext(), next()
Iterator’s method hasNext(), next() can be implemented based on the Object types that it is holding.
Most of the Java collections have Iterators. writing a custom Iterator Should be useful when accessing elements of a collection object in a sequential manner without any need to know its underlying representation
Decorator Design Pattern [Structural]
Decorate one Object with Other Object, pizza Object is the best example from Head First Design Patterns Book.
we have lots of options while ordering pizza like farmhouse, margarita, and toppings. so, it will be difficult in deciding the price and change it.
For Example, we need Margherita with Tomato Toppings. we can simply write like below if we are using a decorator design pattern inside.
Pizza pizza = new Margherita();
pizza = new FreshTomato(pizza);
Let’s how it is Objects are arranged internally,
pizza to Margherita is general Inheritance
Now we need toppings,
By Using Decorator Objects we can create many combinations of classes for pizzas.
Please comment if any corrections or improvements need to be done.