Core Java Design Pattern : Behavioral Pattern : Strategy Design Pattern
Strategy Design Pattern |
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Also Known As : Policy
Many algorithms exist for breaking a stream of text into lines. Hard-wiring all such algorithms into the classes that require them isn’t desirable for several reasons:
- Clients that need linebreaking get more complex if they include the linebreaking code. That makes clients bigger and harder to maintain, especially if they support multiple linebreaking algorithms.
- Different algorithms will be appropriate at different times. We don’t want to support multiple linebreaking algorithms if we don’t use them all.
- It’s difficult to add new algorithms and vary existing ones when linebreaking is an integral part of a client.
We can avoid these problems by defining classes that encapsulate different linebreaking algorithms. An algorithm that’s encapsulated in this way is called a strategy.
Use the Strategy pattern when
- many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.
- you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms.
- an algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.
- a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
“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.”
Strategy (Compositor)
- declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
ConcreteStrategy (SimpleCompositor, TeXCompositor, ArrayCompositor)
- implements the algorithm using the Strategy interface.
Context (Composition)
- is configured with a ConcreteStrategy object.
- maintains a reference to a Strategy object.
- may define an interface that lets Strategy access its data.
Examples |
SortInterface.java
package com.javaskool.sortingStrategy;
public interface SortInterface
{public void sort(int[] list);}
BubbleSort.java
package com.javaskool.sortingStrategy;
public class BubbleSort implements SortInterface {
public void sort(int[] list) {
int temp;
for(int i = 0;i < list.length; i++) {
for(int j = 0; j < list.length - 1; j++) {
if(list[i] < list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}
QuickSort.java
package com.javaskool.sortingStrategy;
public class QuickSort implements SortInterface {
public void sort(int[] a) {
quicksort(a, 0, a.length - 1);
}
private void quicksort(int[] a, int left, int right) {
if (right <= left) return;
int i = partition(a, left, right);
quicksort(a, left, i-1);
quicksort(a, i+1, right);
}
private int partition(int[] a, int left, int right) {
int i = left; int j = right;
while (true) {
while (a[i] < a[right])
i++;
while (less(a[right], a[--j]))
if (j == left)
break;
if (i >= j)
break;
exch(a, i, j);
}
exch(a, i, right);
return i;
}
private boolean less(int x, int y)
{
return (x < y);
}
private void exch(int[] a, int i, int j){
int swap = a[i];
a[i] = a[j]; a[j] = swap;
}
}
SortingContext.java
package com.javaskool.sortingStrategy;
public class SortingContext {
private SortInterface sorter = null;
public void sortInt(int[] list) {
sorter.sort(list);
}
public SortInterface getSorter() {
return sorter;
}
public void setSorter(SortInterface sorter) {
this.sorter = sorter;
}
}
TestDrive.java
package com.javaskool;
import com.javaskool.sortingStrategy.BubbleSort;
import com.javaskool.sortingStrategy.SortingContext;
public class TestDrive {
public static void main(String[] args) {
int[] list = {1,2,7,3,1,0,10,14,12,16,17,21,19};
SortingContext context = new SortingContext();
context.setSorter(new BubbleSort());
context.sortInt(list);
for(int i =0; i < list.length; i++) {
System.out.print(list[i]+", ");
}
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
0, 1, 1, 2, 3, 7, 10, 12, 14, 16, 17, 19, 21,
Recent Comments