In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives runtime instructions as to which in a family of algorithms to use.
Strategy lets the algorithm vary independently from clients that use it.Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates, Head First Design Patterns, First Edition, Chapter 1, Page 24, O'Reilly Media, Inc, 2004. Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al.
For instance, a class that performs validation on incoming data may use the strategy pattern to select a validation algorithm depending on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known until runtime and may require radically different validation to be performed. The validation algorithms (strategies), encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without Duplicate code.
Typically, the strategy pattern stores a reference to code in a data structure and retrieves it. This can be achieved by mechanisms such as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection.
[[Image:Strategy pattern in LePUS3.gif|thumb|none|400px|Strategy pattern in LePUS3 ( legend)]]
As an example, consider a car class. Two possible functionalities for car are brake and accelerate. Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks; accelerate and brake behaviors must be declared in each new car model. The work of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.
The strategy pattern uses composition instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at runtime as well as at design-time. For instance, a car object's brake behavior can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to:
/* Encapsulated family of Algorithms
class BrakeWithABS implements IBrakeBehavior {
class Brake implements IBrakeBehavior {
// Client that can use the algorithms above interchangeably
abstract class Car {
// Client 1 uses one algorithm (Brake) in the constructor
class Sedan extends Car {
// Client 2 uses another algorithm (BrakeWithABS) in the constructor
class SUV extends Car {
// Using the Car example
public class CarExample {
* Interface and its implementations
*/
interface IBrakeBehavior {
public void brake();
}
public void brake() {
System.out.println("Brake with ABS applied");
}
}
public void brake() {
System.out.println("Simple Brake applied");
}
}
private IBrakeBehavior brakeBehavior;
public Car(IBrakeBehavior brakeBehavior) {
this.brakeBehavior = brakeBehavior;
}
public void applyBrake() {
brakeBehavior.brake();
}
public void setBrakeBehavior(IBrakeBehavior brakeType) {
this.brakeBehavior = brakeType;
}
}
public Sedan() {
super(new Brake());
}
}
public SUV() {
super(new BrakeWithABS());
}
}
public static void main(String[] arguments) {
Car sedanCar = new Sedan();
sedanCar.applyBrake(); // This will invoke class "Brake"
Car suvCar = new SUV();
suvCar.applyBrake(); // This will invoke class "BrakeWithABS"
// set brake behavior dynamically
suvCar.setBrakeBehavior(new Brake());
suvCar.applyBrake(); // This will invoke class "Brake"
}
}
|
|