Monday, November 2, 2015

Design Patterns

Mediator


Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which handles all the communications between different classes and supports easy maintainability of the code by loose coupling.
Ex  Arport Control Tower


Chain of Responsibility



The chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request. This pattern comes under behavioral patterns.
In this pattern, normally each receiver contains reference to another receiver. If one object cannot handle the request then it passes the same to the next receiver and so on.

Ex: timesheet approval

Template

In Template pattern, an abstract class exposes defined way(s)/template(s) to execute its methods. Its subclasses can overrides the method implementations as per need basis but the invocation is to be in the same way as defined by an abstract class.  The template method pattern is a very good example when to make a method as final.

Provide an abstract definition for a method or a class and redefine its behavior later or on the fly without changing its structure.


Observer


Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.

Observer pattern uses three actor classes. Subject, Observer and Client. Subject, an object having methods to attach and de-attach observers to a client object.

http://javapapers.com/design-patterns/observer-design-pattern/
http://www.tutorialspoint.com/design_pattern/observer_pattern.htm


In observer design pattern multiple observer objects registers with a subject for change notification. When the state of subject changes, it notifies the observers. Objects that listen or watch for change are called observers and the object that is being watched for is called subject.

Pattern involved is also called as publish-subscribe pattern. Model view controller (MVC) architecture’s core uses the observer design pattern

Subject provides interface for observers to register and unregister themselves with the subject.
Subject knows who its subscribers are.
Multiple observers can subscribe for notifications.
Subject publishes the notifications.
Subject just sends the notification saying the state has changed. It does not pass any state information.

Once the notification is received from subject, observers call the subject and get data that is changed.




State

In State pattern an object behavior changes based on its state. 
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.

An object's behavior change is represented by its member classes, which share the same super class.Ex: My role as Technical Manager, Lead, and Architect http://javapapers.com/design-patterns/state-design-pattern/



Command



 A request is wrapped under a object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command.

Streamlize objects by providing an interface to encapsulate a request and make the interface implemented by subclasses in order to parameterize the clients.


Ex:  java.lang.Runnable and javax.swing.Action

No comments: