Real World Example
A Strategy defines a set of algorithms that can be used interchangeably. Modes of transportation to an airport is an example of a Strategy. Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service. For some airports, subways and helicopters are also available as a mode of transportation to the airport. Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on tradeoffs between cost, convenience, and time.
http://sourcemaking.com/design_patterns/strategy
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
Now, let's take a look at the diagram definition of the Strategy pattern.
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:
1.
//Strategy Interface
2.
public
interface
CompressionStrategy
3.
{
4.
public
void
compressFiles(ArrayList files);
5.
}
And we'll need to provide our two implementations, one for zip and one for rar
01.
public
class
ZipCompressionStrategy
implements
CompressionStrategy
02.
{
03.
04.
public
void
compressFiles(ArrayList files)
05.
{
06.
//using ZIP approach
07.
}
08.
09.
}
01.
public
class
RarCompressionStrategy
implements
CompressionStrategy
02.
{
03.
04.
public
void
compressFiles(ArrayList files)
05.
{
06.
//using RAR approach
07.
}
08.
09.
}
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.
01.
public
class
CompressionContext
02.
{
03.
private
CompressionStrategy strategy;
04.
05.
//this can be set at runtime by the application preferences
06.
public
void
setCompressionStrategy(CompressionStrategy strategy)
07.
{
08.
this
.strategy = strategy;
09.
}
10.
11.
//use the strategy
12.
public
void
createArchive(ArrayList files)
13.
{
14.
strategy.compressFiles(files);
15.
}
16.
17.
}
It's obvious that all the client has to do now is pass through the files to the CompressionContext
01.
public
class
Client
02.
{
03.
04.
public
static
void
main(String[] args)
05.
{
06.
CompressionContext ctx =
new
CompressionContext();
07.
//we could assume context is already set by preferences
08.
ctx.setCompressionStrategy(
new
ZipCompressionStrategy());
09.
//get a list of files
10.
...
11.
ctx.createArchive(fileList);
12.
13.
}
14.
}
No comments:
Post a Comment