Strategy Design Pattern – Java 8

Damith
1,099 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Introduction

We are going to look at how to implement the Strategy Design Pattern in Java. Then, explaining how to leverage Java 8 features to reduce code verbosity. The Strategy design pattern is a behavioral design pattern. This can use for change the behavior of algorithm/logic at runtime.

Implementation

For an example, We need to implement payment logic. A customer can choose credit card or cash as a payment method. But, there are two different logic to calculate final payment.

Create an interface, PaymentStrategy.java

public interface PaymentStrategy {
    double pay(double amount);
}

Then, time to implement the payment logic.

CashPaymentStrategy.java

public class CashPaymentStrategy implements PaymentStrategy {

    @Override
    public double pay(double amount) {
        double serviceCharge = 5.00;
        return amount + serviceCharge;
    }
}

CreditCardPaymentStrategy.java

public class CreditCardStrategy implements PaymentStrategy {

    @Override
    public double pay(double amount) {
        double serviceCharge = 5.00;
        double creditCardFee = 10.00;
        return amount + serviceCharge + creditCardFee;
    }
}

The implementation is done. Now, we are going to test it.

1
23
24
25
26
27
28
29
30
31
32
33
// {...}
public class Main {
public static void main(String[] args) {
double price = 100;
PaymentStrategy paymentStrategy = new CashPaymentStrategy();
System.out.println("Cash Price: - " + paymentStrategy.pay(price));
paymentStrategy = new CreditCardStrategy();
System.out.println("Credit Card Price: - " + paymentStrategy.pay(price));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Leveraging Java 8

In Java 8 has a feature to define static methods in the interface. So that, we are going to use that to reduce code verbosity. First of all, get rid of the CashPaymentStrategy.java and CreditCardPaymentStrategy.java.

public interface PaymentStrategy {

    double pay(double amount);

    static PaymentStrategy cashPayment(){
        double serviceCharge = 5.00;
        return amount -> amount + serviceCharge;
    }

    static PaymentStrategy creditCardPayment(){
        double serviceCharge = 5.00;
        double creditCardFee = 10.00;
        return amount -> amount + serviceCharge + creditCardFee;
    }
}
1
18
19
20
21
22
23
24
25
26
27
28
29
// {...}
public class Main {
public static void main(String[] args) {
double price = 100;
PaymentStrategy paymentStratgy = PaymentStrategy.cashPayment();
System.out.println("Cash Price: - " + paymentStratgy.pay(price));
paymentStratgy = PaymentStrategy.creditCardPayment();
System.out.println("Credit Card Price: - " + paymentStratgy.pay(price));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Original post :- http://mydevgeek.com/strategy-design-pattern-java-8/

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content