1) What is the difference between Factory and Abstract Factory design patttern?
With the Factory pattern, you produce implementations (
Apple
, Banana
, Cherry
, etc.) of a particular interface -- say, IFruit
.IFruitFactory
. Each of those knows how to create different kinds of fruit.2) What is the difference between State and Strategy Pattern?
State pattern changes the behaviour of an object according to its different inner states. For example : the Train cannot open the doors only when the train is the STOPPED state.
Strategy pattern gives the possiblity to write many algorithms and use them on the go. Actually a class can implement different behavioral interfaces designed to be "algorithms" or "Strategies".
For example, for sort algorithms.
3) Difference between Proxy and Decorator design Pattern?
The Decorator adds functions to a basic class to enhance it and improve its performance like BufferReader and BufferWriter they enhance Reader and Writer to add the Bufferized ability.
The Proxy mainly deals with the access to the object.
4) Why Composition is better than Inheritance? Give two reasons
- Because it's easier to add more functionality
- Because in most of times we want the inherit just a part of the behavior from the parent class and not the whole of it
- Because most of languages doesn't permit multiple inheritance
5) Difference between aggregation, association and composition in OOP?
In Aggregation we can delete the parent class and keep the children for example we can in the aggregation of Class and Students. We can delete the Class but the student can still exist.
In the composition, there no way we can talk about the children without the parent class. For example The House is composed from a set of Rooms. But there is no meaning of a room without a house !
In Association, both classes are independant from each other. They can exist without each other.
6) Difference between Template and Strategy design pattern?
Template method pattern: compile-time algorithm selection by subclassing
Strategy pattern: run-time algorithm selection by containment
PS : a great thanks to stackoverflow forums and JavaRevisited Blog in which I found valuable information to collect those questions/answers.