Strategy Design Pattern: Payment System
Design Pattern

Strategy Design Pattern: Payment System

S

Shivam Chauhan

2 months ago

The Strategy Design Pattern is a behavioral design pattern that enables you to define a family of algorithms, encapsulate each one, and make them interchangeable. It allows the algorithm to be selected at runtime, making your code more flexible and easier to extend.

In this article, we’ll cover:

  • What the Strategy Design Pattern is.
  • When and why to use it.
  • How to implement it in Java with real-world examples.
  • How Coudo AI can help you master this pattern.

What is the Strategy Design Pattern?

The Strategy Design Pattern is used to define a set of algorithms and allow the client to choose which algorithm to use at runtime. It separates the algorithm implementation from the client, enabling dynamic selection without modifying the client code.


When to Use the Strategy Design Pattern

The Strategy Pattern is ideal when:

  • You have multiple algorithms for a specific task and want to choose the appropriate one at runtime.
  • You want to avoid using multiple if-else or switch statements in your code.
  • You need to adhere to the Open/Closed Principle, where new algorithms can be added without altering existing code.

Implementing Strategy Design Pattern in Java

Here’s a simple implementation of the Strategy Design Pattern in Java:

Example: Payment System

Let’s build a payment system where users can choose between different payment methods (Credit Card, PayPal, or Google Pay).

java
// Strategy Interface
public interface PaymentStrategy {
    void pay(int amount);
}

// Concrete Strategy 1: Credit Card Payment
public class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " using Credit Card.");
    }
}

// Concrete Strategy 2: PayPal Payment
public class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " using PayPal.");
    }
}

// Concrete Strategy 3: Google Pay Payment
public class GooglePayPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " using Google Pay.");
    }
}

// Context Class
public class PaymentContext {
    private PaymentStrategy paymentStrategy;

    // Set the strategy at runtime
    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    // Execute the strategy
    public void pay(int amount) {
        if (paymentStrategy == null) {
            throw new IllegalStateException("Payment strategy is not set.");
        }
        paymentStrategy.pay(amount);
    }
}

// Client Code
public class Client {
    public static void main(String[] args) {
        PaymentContext context = new PaymentContext();

        // Pay using Credit Card
        context.setPaymentStrategy(new CreditCardPayment());
        context.pay(100);

        // Pay using PayPal
        context.setPaymentStrategy(new PayPalPayment());
        context.pay(200);

        // Pay using Google Pay
        context.setPaymentStrategy(new GooglePayPayment());
        context.pay(300);
    }
}

Explanation:

  1. PaymentStrategy Interface: Defines the common interface for all payment strategies.
  2. Concrete Strategies: Implement the PaymentStrategy interface for specific payment methods.
  3. PaymentContext Class: Encapsulates the PaymentStrategy and allows dynamic switching at runtime.
  4. Client Code: Demonstrates how to set and execute different strategies dynamically.

UML Diagram for Strategy Design Pattern

Here’s the UML diagram representing the Strategy Pattern for the Payment System:

Drag: Pan canvas

Benefits of the Strategy Pattern

  • Open/Closed Principle: Add new strategies without modifying existing code.
  • Avoid Conditional Logic: Eliminates if-else and switch statements for selecting algorithms.
  • Code Reusability: Strategies are modular and reusable across multiple contexts.

Practice Strategy Design Pattern on Coudo AI

On Coudo AI, you can:

  • Solve real-world problems that involve dynamic behavior selection.
  • Visualize and debug the Strategy Pattern with interactive UML diagrams.
  • Receive AI-driven feedback to refine your implementations.

Conclusion

The Strategy Design Pattern is a powerful tool for creating flexible and extensible systems. By encapsulating algorithms and making them interchangeable, this pattern allows for dynamic behavior selection and cleaner code. Start practicing the Strategy Pattern on Coudo AI and master this essential design pattern.

About the Author

S

Shivam Chauhan

Sharing insights about system design and coding practices.