Strategy Pattern in Java


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.


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

/* */


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() {;
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());;


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.

Strategy Pattern in Java
3 (60%) 1 vote

(royal52) is a software engineer. He is good at authoritative link building, Content Writing, SEO, Web Development, Wordpress, HTML, CSS, JAVASCRIPT, Game Development, Unity3d, Graphic Designing, Logo Designing, Android Development and Ios Development. Last but not least, he is passionate about development and gaming.

We will be happy to hear your thoughts

Leave a Reply

Grab our Partner's Top Rated web hosting deal - Upto 90% off