Java - Interview Questions and Answers on Design Patterns

Q1.  What Design pattern Wrapper Classes implement ?

Ans. Adapter.

Q2.  Difference between Factory and Abstract Factory Design Pattern ?

Ans. Factory Pattern deals with creation of objects delegated to a separate factory class whereas Abstract Factory patterns works around a super-factory which creates other factories.

Q3.  Difference between Factory and Builder Design Pattern ?

Ans. Builder pattern is the extension of Factory pattern wherein the Builder class builds a complex object in multiple steps.

Q4.  Difference between Proxy and Adapter ?

Ans. Adapter object has a different input than the real subject whereas Proxy object has the same input as the real subject. Proxy object is such that it should be placed as it is in place of the real subject.

Q5.  Difference between Adapter and Facade ?

Ans. The Difference between these patterns in only the intent. Adapter is used because the objects in current form cannot communicate where as in Facade , though the objects can communicate , A Facade object is placed between the client and subject to simplify the interface.

Q6.  Difference between Builder and Composite ?

Ans. Builder is a creational Design Pattern whereas Composite is a structural design pattern. Composite creates Parent - Child relations between your objects while Builder is used to create group of objects of predefined types.

Q7.  Example of Chain of Responsibility Design Pattern ?

Ans. Exception Handling Throw mechanism.

Q8.  Example of Observer Design Pattern ?

Ans. Listeners.

Q9.  Difference between Factory and Strategy Design Pattern ?

Ans. Factory is a creational design pattern whereas Strategy is behavioral design pattern. Factory revolves around the creation of object at runtime whereas Strategy or Policy revolves around the decision at runtime.

Q10.  Shall we use abstract classes or Interfaces in Policy / Strategy Design Pattern ?

Ans. Strategy deals only with decision making at runtime so Interfaces should be used.