Gang of Four Behavioural pattern: Strategy
Behavioural Pattern
Encapsulates an algorithm inside a class. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
The Strategy Pattern allows a strategy for doing something to be swapped with another Strategy at runtime.
To get the code for this example:
git clone https://github.com/spotadev/gangoffour.git
In src/main/java navigate to this package:
com.javaspeak.designpatterns.go4.behavioural.strategy
You can run the code from the main method of:
StrategyApplication
In this example we have a StrategyContext (ApplicationStrategy) where we set the Strategy. We then call the executeStrategy() which in turn calls the drawShape() method on the underlying Strategy.
In this way, when we have a TriangleStrategy set we end up drawing a Triangle as a shape and when we have a SquareStrategy set we draw a Triangle.
package com.javaspeak.designpatterns.go4.behavioural.strategy;
/**
* Text book description:
* <ul>
* Strategy: Encapsulates an algorithm inside a class. Define a family of algorithms,
* encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary
* independently from clients that use it.
* </ul>
* The Strategy Pattern allows a strategy for doing something to be swapped with another Strategy
* at runtime.
* <p>
* In this example we have a StrategyContext (ApplicationStrategy) where we set the Strategy. We
* then call the executeStrategy() which in turn calls the drawShape() method on the underlying
* Strategy.
* <p>
* In this way, when we have a TriangleStrategy set we end up drawing a Triangle as a shape and
* when we have a SquareStrategy set we draw a Triangle.
*
* @author John Dickerson - 22 Feb 2020
*/
public class StrategyApplication implements StrategyContext {
private Strategy strategy;
@Override
public void setStrategy( Strategy strategy ) {
this.strategy = strategy;
}
@Override
public void executeStrategy() {
strategy.drawShape();
}
/**
* Set a SquareStrategy and call the drawShape() method which in turn draws a Square. Then
* set a TriangleStrategy which calls the drawShape() method which in turn draws a Triangle.
*/
public void runExample() {
setStrategy( new SquareStrategy() );
executeStrategy();
setStrategy( new TriangleStrategy() );
executeStrategy();
}
public static void main( String args[] ) {
StrategyApplication application = new StrategyApplication();
application.runExample();
}
}
package com.javaspeak.designpatterns.go4.behavioural.strategy;
/**
* Different Strategies, such as SquareStrategy and TriangleStrategy can be swapped in to the
* StrategyContext (ApplicationStrategy)
*
* @author John Dickerson - 22 Feb 2020
*/
public interface Strategy {
public void drawShape();
}
package com.javaspeak.designpatterns.go4.behavioural.strategy;
/**
* This interface is optional in the Stategy pattern. It provides a contract for the application
* using the strategy pattern to swap in Strategies and execute them.
*
* @author John Dickerson - 22 Feb 2020
*/
public interface StrategyContext {
/**
* Set the strategy
*
* @param strategy
* The implementation of the strategy provides an implementation of the
* drawShape(..) method
*/
public void setStrategy( Strategy strategy );
public void executeStrategy();
}
package com.javaspeak.designpatterns.go4.behavioural.strategy;
/**
* @author John Dickerson - 22 Feb 2020
*/
public class SquareStrategy implements Strategy {
@Override
public void drawShape() {
StringBuilder sb = new StringBuilder( "\n" );
sb.append( "X X X X\n" );
sb.append( "X X\n" );
sb.append( "X X\n" );
sb.append( "X X X X\n" );
System.out.println( sb.toString() );
}
}
package com.javaspeak.designpatterns.go4.behavioural.strategy;
/**
* @author John Dickerson - 22 Feb 2020
*/
public class TriangleStrategy implements Strategy {
@Override
public void drawShape() {
StringBuilder sb = new StringBuilder( "\n" );
sb.append( " x \n" );
sb.append( " x x \n" );
sb.append( " x x \n" );
sb.append( " x x \n" );
sb.append( "x x x x x\n" );
System.out.println( sb.toString() );
}
}
Back: Gang of Four
Page Author: JD