Design Patterns Uncovered: The Strategy Pattern

Having focussed on the two factory patterns over the last week, today we’ll take a look at the Strategy Pattern, a useful pattern in changing algorithm implementations at runtime, without causing tight coupling.

Strategy in the Real World

To explain the strategy in the real world, let’s take the example of a software developer. If language isn’t an issue I might ask a developer to write a piece of code for me to create a user interface. One developer’s chosen language is Java, so he’ll develop the UI with Swing. Meanwhile, the other developer decides to use C#. I don’t mind, I’ve left the details of how to write the UI to the developers, and both have applied their own strategy. At any stage, the developer chould change their strategy, choosing to use a different language if they feel it’s necessary. It’s all about dynamically changing behaviours.

Design Patterns Refcard
For a great overview of the most popular design patterns, DZone’s Design Patterns Refcard is the best place to start.

The Strategy Pattern

The Strategy pattern is known as a behavioural pattern – it’s used to manage algorithms, relationships and responsibilities between objects. The definition of Strategy provided in the original Gang of Four book on Design Patterns states:

Defines a set of encapsulated algorithms that can be swapped to carry out a specific behaviour

Now, let’s take a look at the diagram definition of the Strategy pattern.

In the above diagram Context is composed of a Strategy. The context could be anything that would require changing behaviours – a class that provides sorting functionality perhaps. The Strategy is simply implemented as an interface, so that we can swap ConcreteStrategys in and out without effecting our Context.

Let’s take a look at how some client might put the Strategy pattern into action:

Use of the Context from the client may vary – your client could tell the Context which strategy it would like to use, or the Context could decide on behalf of the client.  In my opinion, it’s better to leave this decision to the Context, as it removes the type switch statements that we saw in our Factory patterns.

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.

So How Does It Work In Java?

Let’s use the example of a file compression tool – where we create either zip or rar files. First we’ll need a strategy:

//Strategy Interfacepublic interface CompressionStrategy{   public void compressFiles(ArrayList<File> files);}

And we’ll need to provide our two implementations, one for zip and one for rar

public class ZipCompressionStrategy implements CompressionStrategy{

   public void compressFiles(ArrayList<File> files)   {     //using ZIP approach   }

}
public class RarCompressionStrategy implements CompressionStrategy{

   public void compressFiles(ArrayList<File> files)   {     //using RAR approach   }

}

Our context will provide a way for the client to compress the files. Let’s say that there is a preferences setting in our application that sets which compression algorithm to use. We can change our strategy using the setCompressionStrategy method in the Context.

public class CompressionContext{   private CompressionStrategy strategy;   

   //this can be set at runtime by the application preferences   public void setCompressionStrategy(CompressionStrategy strategy)    {       this.strategy = strategy;     }

  //use the strategy   public void createArchive(ArrayList<File> files)    {        strategy.compressFiles(files);   }

}

It’s obvious that all the client has to do now is pass through the files to the CompressionContext

public class Client{

   public static void main(String[] args)   {      CompressionContext ctx = new CompressionContext();      //we could assume context is already set by preferences       ctx.setCompressionStrategy(new ZipCompressionStrategy());          //get a list of files     ...     ctx.createArchive(fileList);    

   }}

Find Out More


The classic Design Patterns: Elements of Reusable Object Oriented Software
For more practical Java examples: Head First Design Patterns.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>