Back
Close

Strategy Design Pattern – Java 8

Damith
12.6K views

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.

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;
    }
}

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

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io