GPT带我学-设计模式-13策略模式

概述

策略模式

例子

你可以创建一个策略工厂(Strategy Factory)来根据传入的 orgId 动态地选择合适的策略。以下是实现示例:

  1. 首先,定义策略接口和具体策略类:
public interface CardPathStrategy {
    String generatePath();
}

public class OrganizationAStrategy implements CardPathStrategy {
    @Override
    public String generatePath() {
        return "path/for/organizationA";
    }
}

public class OrganizationBStrategy implements CardPathStrategy {
    @Override
    public String generatePath() {
        return "path/for/organizationB";
    }
}
  1. 创建一个策略工厂 CardPathStrategyFactory 来根据 orgId 返回相应的策略:
import java.util.HashMap;
import java.util.Map;

public class CardPathStrategyFactory {
    private static final Map<String, CardPathStrategy> strategies = new HashMap<>();

    static {
        strategies.put("orgA", new OrganizationAStrategy());
        strategies.put("orgB", new OrganizationBStrategy());
        // 添加更多策略
    }

    public static CardPathStrategy getStrategy(String orgId) {
        return strategies.get(orgId);
    }
}
  1. 在你的业务逻辑中使用策略工厂:
public class CardPathGenerator {
    public static void main(String[] args) {
        String orgId = "orgA";  // 这个可以是传入的参数

        CardPathStrategy strategy = CardPathStrategyFactory.getStrategy(orgId);
        if (strategy == null) {
            throw new IllegalArgumentException("No strategy found for orgId: " + orgId);
        }

        CardPathContext context = new CardPathContext(strategy);
        System.out.println(context.generateCardPath());  // 输出:path/for/organizationA

        orgId = "orgB";  // 更改组织ID
        strategy = CardPathStrategyFactory.getStrategy(orgId);
        if (strategy == null) {
            throw new IllegalArgumentException("No strategy found for orgId: " + orgId);
        }

        context.setStrategy(strategy);
        System.out.println(context.generateCardPath());  // 输出:path/for/organizationB
    }
}

通过这种方式,你可以根据传入的 orgId 动态地选择不同的策略。工厂类 CardPathStrategyFactory 负责管理策略的创建和选择逻辑,这使得代码更简洁且易于维护。

相关推荐

  1. GPT-设计模式-13策略模式

    2024-07-10 08:26:02       30 阅读
  2. GPT-设计模式11-组合模式

    2024-07-10 08:26:02       39 阅读
  3. GPT-设计模式12-状态模式

    2024-07-10 08:26:02       31 阅读
  4. GPT-设计模式14-工厂模式

    2024-07-10 08:26:02       20 阅读
  5. 笨蛋设计模式行为型模式-策略模式16

    2024-07-10 08:26:02       49 阅读
  6. 设计模式(19):策略模式

    2024-07-10 08:26:02       37 阅读
  7. 跟着GPT设计模式模板模式

    2024-07-10 08:26:02       25 阅读
  8. 设计模式14、strategy 策略模式

    2024-07-10 08:26:02       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 08:26:02       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 08:26:02       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 08:26:02       90 阅读
  4. Python语言-面向对象

    2024-07-10 08:26:02       98 阅读

热门阅读

  1. 写一个字符设备的驱动步骤

    2024-07-10 08:26:02       30 阅读
  2. Transformer和Bert的原理是什么

    2024-07-10 08:26:02       29 阅读
  3. 使用tkinter 制作工作流ui

    2024-07-10 08:26:02       25 阅读
  4. postman工具介绍

    2024-07-10 08:26:02       26 阅读
  5. vue-路由自动化

    2024-07-10 08:26:02       21 阅读
  6. el-date-picker 扩展

    2024-07-10 08:26:02       26 阅读
  7. Go语言入门之变量、常量、指针以及数据类型

    2024-07-10 08:26:02       22 阅读
  8. Kotlin 处理livedata数据倒灌

    2024-07-10 08:26:02       25 阅读
  9. 针对vue3的render函数添加自定义指令

    2024-07-10 08:26:02       28 阅读
  10. Kotlin中的关键字

    2024-07-10 08:26:02       28 阅读
  11. Matlab 使用

    2024-07-10 08:26:02       29 阅读
  12. AI学习指南机器学习篇-层次聚类原理

    2024-07-10 08:26:02       31 阅读
  13. k8s-第一节-minikube

    2024-07-10 08:26:02       27 阅读