Home
Core Java
Java
JDBC
Servlets
JSP
EJB
JMS
Struts
Spring
Hibernate
JSF
RMI
CORBA
J2ME
Performance
Tomcat
Weblogic
Design Patterns
Junit
XML
UML
DB2 & SQL
ANT
Free Gift
Contact Us

XML RSS
What is this?
Add to My Yahoo!
Add to My MSN
Add to Google
 

Behavioral Pattern

Strategy Pattern

Your Ad Here

Creational patterns Structural patterns Behavioral pattern J2EE patterns
Abstract Factory Adapter  Chain of Responsibility
MVC 
Builder Bridge  Command  Business Delegate
Factory method Composite  Interpreter  Composite Entity
Prototype Decorator  Iterator  Data Access Object
Singleton Façade
Mediator  Front Controller

Flyweight  Memento  Intercepting Filter

Proxy  Observer  Service Locator


State  Transfer Object


Strategy 


Template Method



Visitor 

Strategy Pattern



Define strategy pattern
Group several algorithms in a single module to provide alternatives. Also known as policy.

Where to use & benefits
Encapsulate various algorithms to do more or less the same thing.
Need one of several algorithms dynamically.
The algorithms are exchangeable and vary independently
Configure a class with one of many related classes (behaviors).
Avoid exposing complex and algorithm-specific structures.
Data is transparent to the clients.
Reduce multiple conditional statements.
Provide an alternative to subclassing.

Related patterns include
  • State, which can activate several states, whereas a strategy can only activate one of the algorithms.
  • Flyweight, which provides a shared object that can be used in multiple contexts simultaneously, whereas a strategy focuses on one context.
  • Decorator, which changes the skin of an object, whereas a strategy changes the guts of an object.
  • Composite, which is used to combine with a strategy to improve efficiency.

Your Ad Here
Example of strategy pattern
Compress files using different algorithms or save files in different formats or draw graphic in different presentations. Here is a simple example. Just to show the concept of a strategy pattern.

interface FortuneCookies {
    public void print();
}
class Five implements FortuneCookies {
    public void print() {
        System.out.println("It is your turn to get it");
    }
}
class Two implements FortuneCookies {
    public void print() {
        System.out.println("It is never too late to start");
    }
}
class Null implements FortuneCookies {
    public void print() {
        System.out.println("You got nothing");   
    }
}
class Dice {
   public int throwIt() {
       return (int)(Math.random()*6)+1;
   }
}
//more class...
class Test { 
    static void goodFortune() {
        int luckyNum = new Dice().throwIt();
        FortuneCookies fc;
        switch (luckyNum) {
            case 2: fc = new Two();
                    break;
            case 5: fc = new Five();
                    break;
            //more
            default: fc = new Null();
        }
        fc.print();
    }
    public static void main(String[] args) {
        goodFortune();
    }
}


Your Ad Here




footer for Strategy Pattern page