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 ...

Abstract Factory Pattern

to be added ...

Factory Method Pattern

to be added ...

Simple Factory Pattern

All of the Creational patterns deal with ways to create instance of objects. Your program should not depend on how objects are created and arranged.

In Java, we can create an object by using student = new Student();'

Doing this really amounts to hard coding, depending on how you create the object within your program.
This allows us to write methods that can create different objects and that can be extended to create other newly-developed objects, all without modifying the method's code!

Problem statement for Simple Factory Pattern: You need to create





Solution:
























Monday, 12 July 2010

Singleton pattern

Problem
There must be exactly one instance of a class, and it must be accessible to clients from a well-known access pointExample: Trash in windows

Solution
Create a class that
1) Keeps its constructor private
2) Keeps an instance in a private, static variable
3) Provides a public, static method to retrieve that instance

Implementation code

public class SingletonClass {

private static SingletonClass instance = new SingletonClass ();

public static SingletonClass getSingletonClassObject() {
return instance;
}

private SingletonClass() {
// do sth here ...
}

// more methods ...
}

Test class

public class TestSingtonClass {

public static void main(String[] args) {
  SingletonClass s1 = SingletonClass.getSingletonClass();
  SingletonClass s2 = SingletonClass.getSingletonClass();

  System.out.println(s1);
  System.out.println(s2);
  // use SingletonClass object here ...
}

}

Singleton pattern in JDK
See class java.lang.Runtime

......

public class Runtime {

private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() {
  return currentRuntime;
}

/** Don't let anyone else instantiate this class */
private Runtime() {}

......

}

Recommended books: