Posts

Showing posts from November, 2014

Command design pattern

Image
Type : Behavioral Design Pattern Summary Command pattern separates the client which invokes a method from the object which contains the method. Important points Command pattern decouples requestor (Invoker) of the action from the object that perform the action (Receiver). Command pattern can be used to do undo operations. Command pattern is useful when a history of requests is needed. Command pattern is useful when  \we need callback functionality Advantages it decouples the client from the actual class who performs the operation. We can create new commands to have new behaviour. Undo operations can be designed with command pattern. Disadvantages It increases the number of Classes. If the Receiver class changes we would need to change the Command Class. Details Different  Objects participating  in the pattern are, Command - declares an interface for executing an operation; ConcreteCommand - extends the Command interface, implementing the execute metho

Design Pattern : Composite

Image
Type  Structural Design Pattern Summary In the composite pattern, a tree structure exists where identical operations can be performed on leaves(leaf nodes) and nodes (non-leaf nodes).  It allows a group of object to be treated as an instance of a single object. Advantage Clients use the Component Class Interface to interact with objects in the composite structure If call is made to a Leaf , the request is handled directly. If call is to a Composite , it forwards the request to its child components.   Details A node in a tree is a class that can have children. A node class is a 'composite' class. A leaf in a tree is a 'primitive' class that does not have children. The children of a composite can be leaves or other composites. The leaf class and the composite class share a common ' Component ' interface that defines the common operations that can be performed on leaves and composites. When an operation on a composite is performed, this oper

Observer design pattern

Image
Type : Behavioral Design Pattern Summary In Observer design pattern, an object called subject maintains one to many relationship with Observers, so that once the state of the Subject changes it can automatically inform Observers registered with the subject. Advantage An Object(Subject) can notify other Objects(Observer) without being tightly coupled with Observer and depend on Observer Class. Different instances of the same Subject can register its own list of Observers. Disadvantage Due to the indirect communication between Subject and Observer's it can lead to performance issues. Important Points Multiple Subscribers for a Subject. Subjects know its Subscribers. Generally the Subject triggers the method that updates Observers. New Observers can be added to the System with minimal changes. Details Following are different participants in Observer Pattern Subject (Interface)  Keeps track of its observers  Provides an interface for attaching and det

Visitor Design Pattern

Image
Type   Behavioral Design Pattern Summary  Visitor Design pattern follows the open/closed principle and allows us to add new operations to be added to object structure. Open/Closed principle states that entities should be able to extend its behavior or add need new functionality without changing its own implementation. Important Points Visitor Design Pattern uses Double Dispatch to invoke a method at run time based on the object on which method is defined and its parameter. Visitor represents operations or functionality to be performed on different nodes of an object structure. Visitor Pattern  follows Open/Closed Principle. Useful in adding new behavior or functionality to a object tree, without modifying the original Objects code. Advantage  We can add new functionality to the Hierarchy of Classes without changing the classes. Disadvantage The arguments visiting methods have to be known in advance. Details The participants classes in this patte

Decorator Design Pattern

Image
Structural Design Pattern Summary Decorator design pattern is used to extend or decorate certain objects of a Class (not all objects) statically or at run time. Multiple decorators can act on an object to add the desired behavior to a particular object instance without effecting other instances. Decorator design pattern is a Structural design pattern, it is alternative to inheritance/ Sub classing in java. Java sub classing effects all the instances of the class, and some times it is not desirable. Difference between Decorator and Visitor Decorator design pattern works on a single object. Decorator design pattern helps in enhancing a single object with new functionality or behaviour. Decorator is a Structural design pattern, Visitor is a behavioral design pattern. Visitor works on composite objects. Visitor pattern is used to add a new algorithm or functionality to collection of objects or hierarchy of objects. Details Following are the Participants in this pat