Strategy Pattern in Java

0

Java code development should be done efficiently and java development India experts do it by applying design methodologies into applications. In this article, they will tell about the use of Strategy Pattern in development of java code.

Introduction:-

In Java, to make the code more reliable we should design our code efficiently that can be done by applying design methodologies such as OOPs, OOAD, design principles and patterns into applications.

A behavioural design pattern which makes a strategy when multiple algorithms or services are available for a specific behaviour or task, and the caller or client who is going to use the strategy will decide which algorithm or what service at runtime.

Gof says that – “Defines a set of encapsulated algorithms that can be swapped to carry out a specific behaviour”

Strategy Pattern in Java

Strategy – Defines an interface that has to support or adapt for all possible behaviours or algorithms

Context – Uses the strategy interface, which helps to the client to changing/choosing the behaviour or an algorithm that is defined in the concrete strategies

Concrete Strategy – Defines all the algorithms or services in the respective concrete strategies. Each concrete strategy will implement an algorithm

e.g.

//Strategy
public interface PaymentStrategy {
void pay();
}
//Concrete strategy for debit card
public class DebitCardStrategy implements PaymentStrategy {
public void pay() {
// payment thru debit card
}
}
//Concrete strategy for credit card
public class CreditCardStrategy implements PaymentStrategy {
public void pay() {
// payment thru credit card
}
}
//Concrete strategy for net banking
public class NetbankingStrategy implements PaymentStrategy {
public void pay() {
// payment thru netbanking
}
}
//context for payment
public class PaymentContext {

private PaymentStrategy paymentStrategy;

// The algorithm or behaviour will be set based on the client need
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStragtegy;
}
public void pay() {
paymentStrategy.pay();
}
}
Client decides and use the strategy that they requires,
public class Client {
public static void main(String aa[]) {
PaymentContext context = PaymentContext();
context.setPaymentStrategy(new NetbankingStrategy());
context.pay();
}
}

Conclusion:-

Strategy pattern makes the code more reliable i.e.

There won’t be any change in the implementation when the client wants to pay using debit card, can simply make a call thru payment context with debit card strategy.

In future, we want to add a new feature that pay using paytm, without affecting existing implementation we can simply add a new class paytmStrategy that implements PaymentStrategy and client can use the same.

For queries related to Strategy Pattern, you can anytime make comments below. Java development India based experts will answer your questions related to Strategy Pattern via comments.

3/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply