策略模式简单使用

时间:2024-03-09 08:43:37

策略接口

public interface OrderStrategy {

    String getType();

    void handleOrder(Order order);

}

策略实现类

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

获取策略工厂

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

}

执行策略

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

策略实体

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

测试

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

}

结果