Strategy Pattern
Definition
Use strategy when you need to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Related patterns include State, Flyweight, Decorator, Composite.Where to use
•When you need to use one of several algorithms dynamically.•When you want to configure a class with one of many related classes (behaviors).
•When an algorithm uses data that clients shouldn't know about.
Benefits
•Reduces multiple conditional statements in a client.•Hides complex, algorithmic-specific data from the client.
•Provides an alternative to subclassing.
•Can be used to hide data that an algorithm uses that clients shouldn't know about.
Drawbacks/consequences
•Clients must be aware of different strategies. A client must understand how strategies differ before it can select the appropriate one.•Increases the number of objects in an application.
Strategy Pattern Class Diagram
In the class-diagram above:
•The Strategy interface defines the behavior that is common to all the concrete implementations.
•The ConcreteStrategy encapsulates an implementation of a specific algorithm or behavior that is defined through the Strategy interface.
•The Context provides certain services that is defined by the Strategy interface and implemented by different ConcreteStrategy classes depending on behavior.
Strategy Pattern Example
This example uses sorting algorithms. Two sorting algorithms (Bubble sort and Quick sort) are implemented and the client can select either of the algorithms. The SortInterface describes what the algorithms must be able to do, sort(). The classes QuickSort and BubbleSort both implements theSortInterface and each have their own algorithm for sorting. SortingContext maintains a reference to a Strategy object and forwards client requests to the strategy. SortingClient set the concrete strategy in the context and invokes the context to run the algorithm.
public interface SortInterface { public void sort(double[] list); } public class QuickSort implements SortInterface { public void sort(double[] u) { sort(u, 0, u.length - 1); } private void sort(double[] a, int left, int right) { if (right <= left) return; int i = part(a, left, right); sort(a, left, i-1); sort(a, i+1, right); } private int part(double[] a, int left, int right) { int i = left; int j = right; while (true) { while (a[i]< a[right]) i++; while (smaller(a[right], a[--j])) if (j == left) break; if (i >= j) break; swap(a, i, j); } swap(a, i, right); return i; } private boolean smaller(double x, double y) { return (x < y); } private void swap(double[] a, int i, int j) { double swap = a[i]; a[i] = a[j]; a[j] = swap; } } public class BubbleSort implements SortInterface { public void sort(double[] list) { //Bubblesort algorithm here } } public class SortingContext { private SortInterface sorter = null; public void sortDouble(double[] list) { sorter.sort(list); } public SortInterface getSorter() { return sorter; } public void setSorter(SortInterface sorter) { this.sorter = sorter; } } public class SortingClient { public static void main(String[] args) { double[] list = {1,2.4,7.9,3.2,1.2,0.2,10.2, 22.5,19.6,14,12,16,17}; SortingContext context = new SortingContext(); context.setSorter(new QuickSort()); context.sortDouble(list); for(int i =0; i< list.length; i++) { System.out.println(list[i]); } } }
When SortingClient is executed the result is:
c:>0.2
c:>1.0
c:>1.2
c:>2.4
c:>3.2
c:>7.9
c:>10.2
c:>12.0
c:>14.0
c:>16.0
c:>17.0
c:>19.6
c:>22.5
Strategy Pattern Usage Example
The strategy pattern is used in the implementation of the LayoutManager in Java. The LayoutManager can be configured to work with different layout objects, such as FlowLayout, CardLayout, GridLayout, etc. These classes encapsulate the actual algorithms for laying out visual components that the LayoutManager uses to render the interface on the screen. By changing theLayout (algorithm) we can dynamically change how the interface is rendered.
0 comments:
Post a Comment