Design Pattern Interview Questions and Answers Part-4
Service – Actual Service which will process the request. Reference of such service is to be looked upon in JNDI server.
Context / Initial Context – JNDI Context carries the reference to service used for lookup purpose.
Service Locator – Service Locator is a single point of contact to get services by JNDI lookup caching the services.
Cache – Cache to store references of services to reuse them.
Client – Client is the object that invokes the services via ServiceLocator.
32.What is Mediator pattern?
Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling. Mediator pattern falls under behavioral pattern category.
33.What is Memento pattern?
Memento pattern is used to restore state of an object to a previous state. Memento pattern falls under behavioral pattern category.
34.Name the actor classes used in Memento pattern.
Memento pattern uses three actor classes. Memento contains state of an object to be restored. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento.
35.What is Observer pattern?
Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.
36.Name the actor classes used in Observer pattern.
Observer pattern uses three actor classes. Subject, Observer and Client. Subject is an object having methods to attach and detach observers to a client object. We have created an abstract class Observer and a concrete class Subject that is extending class Observer.
37.What is state pattern?
In State pattern a class behavior changes based on its state. This type of design pattern comes under behavior pattern. In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.
38.What is Null Object pattern?
In Null Object pattern, a null object replaces check of NULL object instance. Instead of putting if check for a null value, Null Object reflects a do nothing relationship. Such Null object can also be used to provide default behaviour in case data is not available.
In Null Object pattern, we create an abstract class specifying various operations to be done, concrete classes extending this class and a null object class providing do nothing implemention of this class and will be used seemlessly where we need to check null value.
39.What is Strategy pattern?
In Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern comes under behavior pattern.
In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.
40.What is Template pattern?
In Template pattern, an abstract class exposes defined way(s)/template(s) to execute its methods. Its subclasses can override the method implementation as per need but the invocation is to be in the same way as defined by an abstract class. This pattern comes under behavior pattern category.