A powerful tool for exploring flexibility and maintainability: Detailed explanation of the Strategy pattern

Table of contents

Edit

1. Overview of Strategy Pattern:

2. Main characters:

3. Example scenario:

4. Specific implementation steps:

Step 1: Define the policy interface

5. Client code using strategy pattern:

Summarize:

my other blogs


 

1. Overview of Strategy Pattern:

Strategy pattern is a behavioral design pattern that defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other. The strategy pattern allows the algorithm to change independently of the client, allowing the client to choose different algorithms without affecting the client's code.

2. Main characters:

  • Context: Maintains a reference to the policy object and switches between different policies at runtime.

  • Strategy: defines the public interface for all supported algorithms. Usually an interface or abstract class.

  • ConcreteStrategy (specific strategy): implements the strategy interface and provides specific algorithm implementation.

3. Example scenario:

Consider a payment system with different payment strategies based on different payment methods. The strategy mode allows you to add new payment methods without modifying the existing code. You only need to add a new payment strategy.

4. Specific implementation steps:

Step 1: Define the policy interface
// Strategy 接口
public interface PaymentStrategy {
    void pay(int amount);
}

Step 2: Implement specific strategies

// ConcreteStrategy1
public class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " via Credit Card.");
    }
}

// ConcreteStrategy2
public class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " via PayPal.");
    }
}

Step 3: Define context class 

// Context
public class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}

5. Client code using strategy pattern:

public class Client {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();

        // 选择支付策略
        PaymentStrategy creditCardPayment = new CreditCardPayment();
        PaymentStrategy payPalPayment = new PayPalPayment();

        // 设置支付策略
        cart.setPaymentStrategy(creditCardPayment);

        // 进行支付
        cart.checkout(100);

        // 切换支付策略
        cart.setPaymentStrategy(payPalPayment);

        // 进行支付
        cart.checkout(150);
    }
}

 

Summarize:

The strategy pattern encapsulates the algorithm in an independent strategy class so that changes in the algorithm do not affect the client using the algorithm. This flexibility and maintainability make the Strategy pattern very useful when faced with multiple algorithm choices, while improving the scalability of the code.

my other blogs

Git command collection: from basics to advanced applications-CSDN Blog

Briefly introduce some other trees-CSDN Blog

What is tomcat? What is tomcat used for? -CSDN Blog

TCP/IP four-layer architecture-CSDN Blog

Redis new data type-Bitmaps-CSDN blog

Tencent-Insufficient memory when installing MySQL8.0 in Pagoda in centos7, a lightweight application server-CSDN BlogSynchronized Optimization-CSDN BlogTencent-Light Insufficient memory when installing MySQL 8.0 in the pagoda of the mass application server centos7 - CSDN Blog

[Computer Network] URL Concept and Composition-CSDN Blog

[Computer Network] TCP socket programming-CSDN Blog

Final modification of enumeration class-CSDN Blog

What is RabbitMQ-CSDN Blog

 

 

 

Guess you like

Origin blog.csdn.net/AliceNo/article/details/135035471