策略模式简单使用

策略接口

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

}

结果

相关推荐
seasugar9 分钟前
Maven怎么会出现一个dependency-reduced-pom.xml的文件
xml·java·maven
一只淡水鱼6612 分钟前
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
java·数据库·spring·mybatis
唐叔在学习30 分钟前
【唐叔学算法】第19天:交换排序-冒泡排序与快速排序的深度解析及Java实现
java·算法·排序算法
music0ant34 分钟前
Idea 配置环境 更改Maven设置
java·maven·intellij-idea
bloxed39 分钟前
前端文件下载多方式集合
前端·filedownload
余生H44 分钟前
前端Python应用指南(三)Django vs Flask:哪种框架适合构建你的下一个Web应用?
前端·python·django
记得开心一点嘛1 小时前
Nginx与Tomcat之间的关系
java·nginx·tomcat
LUwantAC1 小时前
CSS(四)display和float
前端·css
cwtlw1 小时前
CSS学习记录20
前端·css·笔记·学习
界面开发小八哥1 小时前
「Java EE开发指南」如何用MyEclipse构建一个Web项目?(一)
java·前端·ide·java-ee·myeclipse