Tuesday, 17 August 2010

Adaptor Pattern


Intent
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Motivation
Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application
We can not change the library interface, since we may not have its source code
Even if we did have the source code, we probably should not change the library for each domain-specific application

Solution:














When to use:
You want to use an existing class, and its interface does not match the one you need
You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces

Example in JDK
java.awt.event.MouseAdapter





Friday, 16 July 2010

Chain of Responsibility Pattern

Purpose:




Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

When to use:
When more than one object may handle a request and the actual handler is not know in advance
When requests follow a “handle or forward” model - that is, some requests can be handled where they are generated while others must be forwarded to another object to be handled


Consequences
Reduced coupling between the sender of a request and the receiver – the sender and receiver have no explicit knowledge of each other
Receipt is not guaranteed - a request could fall off the end of the chain without being handled
The chain of handlers can be modified dynamically

Structure






Example: Java Servlet Filters













A filter intercepts the request before it gets to the requested resource. A response is returned to the client through the filter.


















Recommended books:

Head First Design PatternsJava(TM) Design Patterns: A Tutorial

State Pattern

to be added ...

Observor Pattern

to be added ...

Composite Pattern

to be added ...

MVC Pattern

to be added...

Facade Pattern

to be added ...