Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Class Diagram:
Code:
public
class
Client{
public
static
void
main(String[] argv){
Context context=
new
Context();
context.setStrategy(
new
ConcreteStrategyA())
context.ContextInterface();
}
}
public
class
Context{
private
Strategy strategy;
public
void
setStrategy(Strategy strategy){
this
.strategy=strategy;
}
public
void
ContextInterface(){
strategy.AlgorithmInterface();
}
}
public
interface
Strategy{
public
void
AlgorithmInterface();
}
public
class
ConcreteStrategyA
implements
Strategy{
public
void
AlgorithmInterface(){
//代码块.....
}
}
public
class
ConcreteStrategyB
implements
Strategy{
public
void
AlgorithmInterface(){
//代码块.....
}
}
public
class
ConcreteStrategyC
implements
Strategy{
public
void
AlgorithmInterface(){
//代码块.....
}
}
Observer
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Class Diagram:
Code:
public interface Subject {
void Attach(Observer observer);
void Detach(Observer observer);
void Notify();
}
public ConcreteSubject implements Subject{
private List observerList=new ArrayList();
void Attach(Observer observer){
this.observerList.add(observer);
}
void Detach(Observer observer){
this.observerList.remove(observer);
}
void Notify(){
for(int i=0;i<observerList.size;i++){
Observer observer=(Observer)observerList.get(i);
observer.update();
}
}
}
public interface Observer{
void update();
}
public class ConcreteObserver implements Observer{
void update(){
//代码什么的。。。。
}
}
public class Client{
public static void main(String argvs[]){
Observer observer=new ConcreteObserver();
Subject subject=new ConcreteSubject ();
subject.add(observer);
subject.Notify();
}
}
Singleton
Ensure a class only has one instance, and provide a global point of access to it.
Class Diagram:
Code:
public class Singleton(){
private Singleton uniqueInstance=new Singleton();
private void Singleton(){
}
public static Singleton Instance(){
return uniqueInstance;
}
}
public class Singleton(){
private Singleton uniqueInstance=null
private void Singleton(){
}
public static synchronized Singleton Instance(){
if(uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}
}
Template Method
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Class Diagram:
Code:
public abstract class AbstractClass{
void TemplateMethod(){
PrimitiveOperation1();
PrimitiveOperation2();
}
void PrimitiveOperation1();
void PrimitiveOperation1();
}
public class ConcreteClass extends AbstractClass{
void PrimitiveOperation1(){
//代码什么的。。。。。
}
void PrimitiveOperation1(){
//代码什么的。。。。。
}
}
public class Client {
public static void main(String argvs[]){
AbstractClass concreteClass=new ConcreteClass ();
concreteClass.TemplateMethod();
}
}
Composite
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Class Diagram:
Code:
public class Client{
public static void main(String argvs[]){
Composite composite=new Composite();
composite.Add(new Leaf());
composite.Operation();
}
}
public interface Component{
void Operation();
}
public class Leaf implements Component{
public void Operation(){
//代码什么的。。。。。
}
}
public class Composite{
private List componentList=new ArrayList();
public void Operation(){
for(int i=0;i<componentList.size();i++){
Component component=(Component )componentList.get(i);
component.Operation();
}
}
public void Add(Component component){
componentList.add(component);
}
public void Remove(Component component){
componentList.remove(component);
}
public Component GetChild(int i){
return (Component )componentList.get(i);
}
}
Iterator
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Iterator provides a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).
Class Diagram:
Code:
public class Client{
public static void main(String argvs[]){
Aggregate aggregate=new ConcreteAggregate();
Iterator iterator=aggregate.CreateIterator();
itertor.First();
}
}
public interface Aggregate{
Iterator CreateIterator();
}
public class ConcreteAggregate implements Aggregate{
public Iterator CreateIterator(){
return new ConcreteIterator(this);
}
}
public interface Iterator{
First();
Next();
}
public class ConcreteIterator implements Iterator{
private ConcreteAggregate concreteAggregate=new ConcreteAggregate ();
public ConcreteItertor(ConcreteAggregate concreteAggregate){
this.concreteAggregate=concreteAggregate;
}
public void First(){
//代码什么的。。。。。
}
public void Next(){
//代码什么的。。。。。
}
}
Decorator
Sometimes we want to add responsibilities to individual objects, not to an entire class. One way to add responsibilities is with inheritance. A more flexible approach is to enclose the component in another object. The enclosing object is called a decorator. The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients. The decorator forwards requests to the component and may perform additional actions (such as drawing a border) before or after forwarding.
Class Diagram:
Code:
public class Client{
public static void main(String argvs[]){
Component component=new ConcreteComponent();
Decorator decorator=new ConcreteDecoratorA(new ConcreteDecoratorB(component));
decorator.Operation();
}
}
public interface Component{
void Operation();
}
public class ConcreteComponent implements Component{
public void Operation(){
//代码什么的。。。。。
}
}
public class Decorator implements Component{
private Component component;
public Decorator(Component component){
this.component=component;
}
public void Operation(){
component.Operation();
}
}
public class ConcreteDecoratorA extends Decorator{
public ConcreteDecoratorA (Component component){
super(component);
}
public void Operation(){
super.Operation();
//代码什么的。。。。
}
}
public class ConcreteDecoratorB extends Decorator{
public ConcreteDecoratorB (Component component){
super(component);
}
public void Operation(){
super.Operation();
//代码什么的。。。。
}
}