[Design pattern] 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, decoupling the client code and the specific implementation of the algorithm. In this way, the client can choose different algorithms according to different requirements without modifying the original code.

C implements the strategy pattern

#include <stdio.h>
#include <stdlib.h>

// 定义策略接口
typedef struct {
    
    
    void (*execute)(void);
} Strategy;

// 具体策略1
void strategy1_execute(void) {
    
    
    printf("Executing Strategy 1\n");
}

// 具体策略2
void strategy2_execute(void) {
    
    
    printf("Executing Strategy 2\n");
}

// 客户端代码,使用策略接口
void client_code(Strategy* strategy) {
    
    
    strategy->execute();
}

int main() {
    
    
    // 创建策略对象
    Strategy* strategy1 = (Strategy*)malloc(sizeof(Strategy));
    strategy1->execute = strategy1_execute;

    Strategy* strategy2 = (Strategy*)malloc(sizeof(Strategy));
    strategy2->execute = strategy2_execute;

    // 使用不同的策略
    client_code(strategy1);
    client_code(strategy2);

    // 释放内存
    free(strategy1);
    free(strategy2);

    return 0;
}

C++ Implements the Strategy Pattern

#include <iostream>

// 定义策略接口
class Strategy {
    
    
public:
    virtual void execute() = 0;
    virtual ~Strategy() {
    
    }
};

// 具体策略1
class Strategy1 : public Strategy {
    
    
public:
    void execute() override {
    
    
        std::cout << "Executing Strategy 1" << std::endl;
    }
};

// 具体策略2
class Strategy2 : public Strategy {
    
    
public:
    void execute() override {
    
    
        std::cout << "Executing Strategy 2" << std::endl;
    }
};

// 客户端代码,使用策略接口
void client_code(Strategy* strategy) {
    
    
    strategy->execute();
}

int main() {
    
    
    // 使用不同的策略
    Strategy1 strategy1;
    Strategy2 strategy2;

    client_code(&strategy1);
    client_code(&strategy2);

    return 0;
}

Advantages and disadvantages of strategy pattern

advantage:

Enhanced flexibility : The strategy pattern enables the algorithm to vary independently of client usage. Algorithms can be dynamically selected at runtime to flexibly respond to different needs and scenarios.
Code reuse : each strategy can be shared by multiple clients, avoiding code duplication.
Good scalability : when a new algorithm needs to be added, it only needs to add a new strategy class without modifying the existing code.
Ease of testing : Since the strategy class encapsulates the concrete algorithm, it is easy to unit test.

shortcoming:

Increase the number of classes : Each specific strategy requires a corresponding class. If there are many strategies, the number of classes may be increased, which increases the complexity of code maintenance.
The client must understand the strategy : the client code must understand all the strategies in order to make a choice. If there are many strategies, the complexity of the client code may increase.
In general , the strategy pattern is suitable for situations where an algorithm needs to be dynamically selected at runtime and the algorithm and client code are expected to be decoupled. For simple cases, it may not be necessary to use the strategy pattern, but in complex scenarios, it can lead to better maintainability and scalability.

Guess you like

Origin blog.csdn.net/qq_43577613/article/details/132005798