策略模式简单使用

策略接口

java 复制代码
public interface OrderStrategy {

    String getType();

    void handleOrder(Order order);

}

策略实现类

java 复制代码
public class JdOrderStrategyImpl implements OrderStrategy {
    @Override
    public String getType() {
        return "JD";
    }

    @Override
    public void handleOrder(Order order) {
        System.out.println(order.getDoSomething());
    }
}

public class PddOrderStrategyImpl implements OrderStrategy {
    @Override
    public String getType() {
        return "PDD";
    }

    @Override
    public void handleOrder(Order order) {
        System.out.println(order.getDoSomething());
    }
}

public class TaobaoOrderStrategyImpl implements OrderStrategy {
    @Override
    public String getType() {
        return "taobao";
    }

    @Override
    public void handleOrder(Order order) {
        System.out.println(order.getDoSomething());
    }
}

获取策略工厂

java 复制代码
public class OrderStrategyFactory {

    private static final Map<String, OrderStrategy> orderStrategyMap;

    static {
        orderStrategyMap = new HashMap<>();
        JdOrderStrategyImpl jdOrderStrategy = new JdOrderStrategyImpl();
        orderStrategyMap.put(jdOrderStrategy.getType(), jdOrderStrategy);
        PddOrderStrategyImpl pddOrderStrategy = new PddOrderStrategyImpl();
        orderStrategyMap.put(pddOrderStrategy.getType(), pddOrderStrategy);
        TaobaoOrderStrategyImpl taobaoOrderStrategy = new TaobaoOrderStrategyImpl();
        orderStrategyMap.put(taobaoOrderStrategy.getType(), taobaoOrderStrategy);
    }

    public static OrderStrategy getOrderStrategy(String type) {
        return orderStrategyMap.get(type);
    }

}

执行策略

java 复制代码
public class OrderStrategyService {

    private OrderStrategy orderStrategy;

    public void setOrderStrategy(OrderStrategy orderStrategy) {
        this.orderStrategy = orderStrategy;
    }

    public void doSomething(Order order) {
        if (null != order) {
            orderStrategy.handleOrder(order);
        }
    }
}

策略实体

java 复制代码
public class Order {

    private String type;

    private String doSomething;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDoSomething() {
        return doSomething;
    }

    public void setDoSomething(String doSomething) {
        this.doSomething = doSomething;
    }
}

测试

java 复制代码
public class OrderTest {

    public static void main(String[] args) {

        List<Order> orders = new ArrayList<>();
        Order order1 = new Order();
        order1.setType("JD");
        order1.setDoSomething("jingdong");
        orders.add(order1);
        Order order2 = new Order();
        order2.setType("taobao");
        order2.setDoSomething("taobao");
        orders.add(order2);
        Order order3 = new Order();
        order3.setType("PDD");
        order3.setDoSomething("pinduoduo");
        orders.add(order3);

        OrderStrategyService orderStrategyService = new OrderStrategyService();

        for (Order order : orders) {
            OrderStrategy orderStrategy = OrderStrategyFactory.getOrderStrategy(order.getType());
            orderStrategyService.setOrderStrategy(orderStrategy);
            orderStrategyService.doSomething(order);
        }
    }

}

结果

相关推荐
这周也會开心几秒前
JDK1.8新增语法
java·开发语言
●VON1 分钟前
使用 Electron 构建天气桌面小工具:调用公开 API 实现跨平台实时天气查询V1.0.0
前端·javascript·electron·openharmony
心随雨下3 分钟前
TypeScript泛型开发常见错误解析
java·开发语言·typescript
码上成长4 分钟前
包管理提速:pnpm + Workspace + Changesets 搭建版本体系
前端·前端框架
DonaldCen6665 分钟前
Java 王者修炼手册【Mysql篇 - SQL执行存储流程】:拆解 InnoDB 存储结构与 SQL 执行流程,吃透 Buffer Pool 和 Change
java
Bigger8 分钟前
Tauri(十九)——实现 macOS 划词监控的完整实践
前端·rust·app
旺仔Sec21 分钟前
2025年广东省职业院校技能大赛高职组“区块链技术应用”竞赛试题(二)
java·区块链
Boop_wu35 分钟前
[Java EE] 多线程编程进阶
java·数据库·java-ee
w***37511 小时前
SpringBoot【实用篇】- 测试
java·spring boot·后端
ganshenml1 小时前
【Web】证书(SSL/TLS)与域名之间的关系:完整、通俗、可落地的讲解
前端·网络协议·ssl