Java — Strategy Design Pattern Example using Java
Strategy Design Pattern defines a set of algorithms, encapsulates the algorithms and makes the client use them interchangeably.
Underlying principles of strategy design pattern
- Separate dynamic part from static part
- Prefer composition over inheritance
- Code to interface and not to implementation
Structure Summary
- Identify an algorithm (i.e. a behavior) that the client would prefer to access
- Specify the signature for that algorithm in an interface
- Bury the alternative implementation details in derived classes
- Clients of the algorithm couple themselves to the interface, not to any particular implementation
The participant classes in the strategy pattern are
Strategy
Defines an interface common to all supported algorithms
ConcreteStrategy
Each concrete strategy implements an algorithm
Context
Context contains a reference to a strategy interface and uses it to call the algorithm defined by a ConcreteStrategy
Client
Clients interacts only with the context
UML Diagram
Implementation Steps
Step 1 — Create the strategy interface
Step 2 — Create concrete strategy classes for different algorithms
Step 3 — Create a context class the references the strategy interface
Step 4 — Create a client class that interacts with the context object (not shown in the above diagram)
Code Example
Step 1:
public interface PaymentStrategy {public void pay(String amount);}
Here we have declared an interface “PaymentStrategy” with the “pay” method which takes “amount” as an argument.
Step 2:
public class CreditCardPaymentStrategy implements PaymentStrategy {public void pay(String amount) {System.out.println(“Customer pays the money “ +
amount + “ USD. using credit card”);}}public class DebitCardPaymentStrategy implements PaymentStrategy {public void pay(String amount) {System.out.println(“Customer pays the money “ +
amount + “ USD. using debit card”);}}public class CashPaymentStrategy implements PaymentStrategy {public void pay(String amount) {System.out.println(“Customer pays the money “ +
amount + “ USD. using cash”);}}
Here we have created various implementation classes namely “CreditCardPaymentStrategy”, “DebitCardPaymentStrategy” and “CashPaymentStrategy” which implements “PaymentStrategy” interface and provided body to the “pay” method.
Step 3:
public class PaymentContext {private PaymentStrategy paymentStrategy;public PaymentContext(PaymentStrategy paymentStrategy) {this.paymentStrategy = paymentStrategy;}public void setPaymentStrategy(PaymentStrategy paymentStrategy) {this.paymentStrategy = paymentStrategy;}public void pay(String amount) {paymentStrategy.pay(amount);}}
Here we have declared a reference to the “PaymentStrategy” interface and initialized it in the constructor. We have also defined “pay” method whose behaviour we want to change dynamically and in the body we have delegated the implementation to one of the concrete classes of the “PaymentStrategy” interface.
Step 4:
public class Customer {public static void main(String… args) {PaymentContext ctx = new PaymentContext(
new CreditCardPaymentStrategy());ctx.pay(8000);ctx.setPaymentStrategy(new CashPaymentStrategy());ctx.pay(6000);}}
Here first we have set the payment strategy of the context object to “CreditCardPaymentStrategy” using the constructor and invoked the “pay” method with the amount as “8000” USD. Later we have set the payment strategy of the context object with the different implementation of payment strategy at runtime to “CashPaymentStrategy” by using the setPaymentStrategy method and invoked the pay method with the amount as “6000” USD.
Verify the output
Customer pays the money 8000 USD. using credit cardCustomer pays the money 6000 USD. using cash
Summary
The Strategy is a behavioral design pattern.
Use the Strategy pattern when you want to alter the object’s behavior at runtime by associating it with different implementations of concrete child objects which can perform specific tasks in different ways.
I hope this explanation and Strategy Pattern code example have been helpful. If you like this tutorial please give a clap.