Q1. How many Java design patterns you have come across and what are those ?
Ans: Singleton, factory, visitor, abstract factory, observer.
Q2. Where do you use singleton pattern and why ?
Ans: If I require a single instance of an object in a particular JVM, like for example
while designing database connection pool. I would require a single connection object
for all the users coming in, not a separate one for each user.
Q3. Why factory pattern is used and an example?
Ans: Factory pattern is used in place where the implementation varies over time.
Factory pattern suggest to create many different instances from interfaces.
Interaces are the one that faces client and implementation of those methods come
from factory depending on a specific condition.
An example: If the OS is Windows, look and feel of the application changes to
Window style, for linux it is Metal and for machintosh it will be different.
Q4. Where do you use visitor pattern and why?
Ans: If I want to defrag business logic in different sets of modules and the final
processing requires all these module to be included in a particular fashion.
Visitor pattern generally calls a visit method of these modules /objects and
all the different logic stored in different modules and call one by one.
It is something like visiting many modules one at a time.
Q5. What problem an observer pattern solves?
Ans: If a particular event has to be notified to many objects, and list grows over time
and it is hardly possible to call /notify each and every listeners at design time.
We use observer pattern , just to register many objects listening to a particular
event and getting notified automatically, as and when the event occurs.
Q6. What is the difference between Model2 and MVC models?
Ans: In model2, we have client tier as jsp, controller is servlet, and business logic is
java bean. Controller and business logic beans are tightly coupled. And controller
receives the UI tier parameters.
But in MVC, Controller and business logic are loosely coupled and controller has
nothing to do with the project/ business logic as such. Client tier parameters are
automatically transmitted to the business logic bean , commonly called as ActionForm.
So Model2 is a project specific model and MVC is project independent.