策略模式简单使用

策略接口

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);
        }
    }

}

结果

相关推荐
懒大王爱吃狼30 分钟前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
阿伟*rui2 小时前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel
XiaoLeisj4 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
paopaokaka_luck4 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
dayouziei4 小时前
java的类加载机制的学习
java·学习
逐·風5 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
Devil枫5 小时前
Vue 3 单元测试与E2E测试
前端·vue.js·单元测试
Yaml45 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~5 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616886 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端