Friday 4 January 2013

Design Patterns- Strategy

OBJECTIVE:
Define a family of algorithms, encapsulate each one, and make them interchangeable at runtime. The Strategy pattern lets the algorithm vary independently from clients that use it.
Gang of Four book on Design Patterns states:
Defines a set of encapsulated algorithms that can be swapped to carry out a specific behaviour at runtime.
The strategy pattern is a software design pattern, whereby an algorithm's behaviour can be selected at runtime.
For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, and/or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.
In the strategy pattern, behaviour are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling between the behaviour and the class that uses the behaviour. The behaviour can be changed without breaking the classes that use it, and the classes can switch between behaviour by changing the specific implementation used without requiring any significant code change behaviour can also be changed at run-time as well as at design-time. For instance, a car object’s brake behaviour can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to:

* This pattern specifies set of classes, each representing a potential behaviour, so switching between those classes changes a application behaviour.
* Each one of those specifies classes, would usually encapsulates an algoritham. so switching between the classes, switch the algorithm, and thus change the behaviour of application.
* so using this pattern makes it easier to add OR remove specific behaviour without having to recode OR retest, all OR parts of application.
Advantages :
The Strategy pattern lets you build software as a loosely coupled collection of interchangeable parts.
That loose coupling makes your software much more extensible, maintainable, and reusable.
This gives greater flexibility in design and is in harmony with the Open/closed principle (OCP) that states that classes should be open for extension but closed for modification.

Where Would I Use This Pattern:
The Strategy pattern is to be used where you want to choose the algorithm to use at runtime. A good use of the Strategy pattern would be saving files in different formats, running various sorting algorithms, or file compression.
The Strategy pattern provides a way to define a family of algorithms, encapsulate each one as an object, and make them interchangeable.
Examples -- http://java.dzone.com/articles/design-patterns-strategy
Examples -- http://en.wikipedia.org/wiki/Strategy_pattern

Example with OUTPUT :

public interface GoAlgorithm {

public void go();
}

------------------------------------------------------------------------------------------
public class GoByDrivingAlgorithm implements GoAlgorithm {

public void go() {

System.out.println(" Now I am Driving ");
}

}

------------------------------------------------------------------------------------------
public class GoByFlyingAlgorithm implements GoAlgorithm {

public void go() {

System.out.println(" Now I am Flying ");
}

}

------------------------------------------------------------------------------------------
public class GoByFastFlyingAlgorithm implements GoAlgorithm {

public void go() {

System.out.println(" Now I am Flying Fast ");
}

}

------------------------------------------------------------------------------------------
public abstract class Vehicle {

private GoAlgorithm goAlgorithm;

public void setGoAlgorithm(GoAlgorithm newGoAlgorithm){
goAlgorithm = newGoAlgorithm;
}

public void go(){
goAlgorithm.go();
}
}

------------------------------------------------------------------------------------------
public class StreetRacer extends Vehicle {

public StreetRacer(){
setGoAlgorithm(new GoByDrivingAlgorithm());
}

}

------------------------------------------------------------------------------------------
public class FormulaOne extends Vehicle {

public FormulaOne(){
setGoAlgorithm(new GoByDrivingAlgorithm());
}
}

------------------------------------------------------------------------------------------
public class HeliCopter extends Vehicle {

public HeliCopter(){
setGoAlgorithm(new GoByFlyingAlgorithm());
}
}

------------------------------------------------------------------------------------------
public class Jet extends Vehicle {

public Jet(){
setGoAlgorithm(new GoByFastFlyingAlgorithm());
}
}

------------------------------------------------------------------------------------------
public class client {

public static void main(String args[]){

Vehicle vehicle = new Jet();

vehicle.go();

vehicle.setGoAlgorithm(new GoByFlyingAlgorithm());

vehicle.go();

vehicle.setGoAlgorithm(new GoByFastFlyingAlgorithm());

vehicle.go();
}
}

------------------------------------------------------------------------------------------
OUTPUT :

Now I am Flying Fast
Now I am Flying
Now I am Flying Fast 

No comments:

Post a Comment