星期一, 四月 30, 2007

Design Patterns - Memento Pattern

Use the Menento Pattern when you need to be able to return an object to one of its previous states ; for instance , if your user requests an "undo".

Design Patterns - Prototype Pattern

Use the prototype pattern when creating an instance of a given class is either expensive or complicated.

星期三, 四月 25, 2007

Design Patterns - Composite Pattern

The Composite Pattern : The Composite Design pattern allows a client object to treat both single components and collections of components identically.

星期一, 四月 09, 2007

Design Patterns - Decorator Pattern

The Decorator Pattern : attaches additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.

Design Patterns - Facade Pattern

The Facade Pattern : provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.


Design Patterns - Adapter Pattern

The Adapter Pattern : converts the interface of a class into another interface the clients expect.
Adapter lets classes work together that couldn't otherwise because of incompatible interface.




Design Patterns - Singleton Pattern

The Singleton Pattern : ensures a class has only one instance , and provides a global point of access it.

sample code :

public class Singleton {

private static Singleton uniqueInstance;

private Singleton() {
}

public Singleton getInstance() {
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}

Deal with multi thread :

public class Singleton {

private static Singleton uniqueInstance;

private Singleton() {
}

public synchronized Singleton getInstance() {
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}

Double checked Singlton method :

package com.oreilly.designpattern.singleton;

public class DoubleCheckSingleton {

private volatile static DoubleCheckSingleton uniqueInstance;

private DoubleCheckSingleton() {

}

public DoubleCheckSingleton getInstance() {
if (uniqueInstance == null) {
synchronized (DoubleCheckSingleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new DoubleCheckSingleton();
}
}
}
return uniqueInstance;
}

}