Design Pattern : Part 2
Earlier in Design Pattern : part 1, We have seen how we can use creational and structural design patterns. In this blog we will cover behaviour design patterns. This design patterns are equally important compare to earlier two and that's the reason it is asked frequently in java interview. The important design patterns are Visitor, Strategy, Command and Observer. Personally I have faced these questions in interview- How we can perform new operation in class without any change in class? Which design pattern is used for undo and redo in text formatter? How to notify object property change to a number of classes?
Chain of Responsibility:
This is a way of passing a request between a chain of objects. In this pattern there is a single chain of passing a request with many handlers. Each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. This pattern is used to process those requests that are varied.
In this pattern we encapsulate a command request as an object. To call a method later we need some information like method name, its object and its values, to represent that information command pattern is used. It allows a request to be saved in a queue. The Command pattern has three main components: the Invoker, the Command, and the Receiver.
This is a way to include language elements in a program. Interpreter pattern defines how the communication between the entities is controlled in a good manner and also defines the instructions grammar. The interpreter design pattern is useful for simple languages where performance is not critical.
It helps us sequentially access the elements of a collection. This pattern is used to provide the way to access the aggregate object without any knowledge of Aggregate structure. The iterator object will maintain the state of the iteration, keeping track of the current item and having a way of identifying what elements are next to be iterated. This allows traversing of lists, trees and other structures in a standard manner.
In this pattern we define simplified communication between classes. Mediator design pattern also deals with communication and defines an object to encapsulate that how the set of an object should interact. It promotes many-to-many relationships between interacting peers and thus defines simplifies communication between classes.
In this pattern we capture and restore an object's internal state. This pattern proves to be helpful in saving the internal state of the object in to external state and enabling us to restore the state later when needed means it easily restore an object to its previous state with minimal lines of coding. Memento pattern helps us to store a snapshot which can be reverted at any moment of time by the object. A word processor providing undo operation or Ctrl+Z is the example of this pattern.
In this we can find out a way of notifying change to a number of classes. This pattern allows us to plug objects into a framework at runtime, which allows for highly flexible, extensible, and reusable software. It is used when it is necessary to ensure that multiple components (called observers or subscribers) are kept in sync with a master set of data (the subject or publisher).
State Pattern allows an object to alter its behavior dynamically according to its internal state. For this we directly move the logic to property "State" which has method Handle() and call this method.
It is useful when we want to encapsulates an algorithm inside a classes. The strategy pattern is very useful when we are in a need of a dynamic formula or data. It is used to separate different types of algorithms that serve a similar purpose. It's a strategy of dealing with a code design with a purpose to decouple concrete code into separate classes, which promotes reusability.
The main intention of this This pattern helps us to improve code reusability. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. We cannot override the template method. Only the steps given in the skeleton of algorithm of template method need to be overridden in concrete classes.
With the help of visitor pattern the new operations are performed on an existing structure. The visitor pattern also specifies how iteration occurs over the object structure. It is a classic technique for recovering lost type information.
Posted by Java Interview Questions