设计模式-状态模式(State)

状态模式是行为模式的一种,状态模式允许改变对象内部状态来改变对象的行为。

角色和职责:

1.上下文(Context)-Order

   拥有内部的状态

2.状态接口(Status)-State:

    一个所有具体状态的共同接口;任何状态都实现这个相同的接口,这样一来状态之间可以互相替换

3.具体状态(Concrete State)-:

   

UML图:

 

具体源码:

 

/**
 * 状态接口类
 */
public abstract class State {
    protected int owner_order_status = 0;//当前状态对象的具体状态
    protected State next_state;//当前状态的下一个状态
    public State(int owner_order_status,State next_state){
        this.owner_order_status = owner_order_status;
        this.next_state = next_state;
    }

    /**
     * 处理
     * @param orderStatus
     */
    protected  void handler(int orderStatus){
        if(!isOwner(owner_order_status,orderStatus)){
            if(next_state != null){
                //非当前对象,继续到下一个对象中查找
                next_state.handler(orderStatus);
                return;
            }
            System.out.println("当前订单状态未定义");
            return;
        }
        diaplay();
    }

    /**
     * 是否当前对象
     * @return
     */
    public boolean isOwner(int owner_order_status,int orderStatus){
        if(owner_order_status == orderStatus){
            return true;
        }
        return false;
    }

    abstract void diaplay();
}

 

/**
 * 订单创建
 */
public class OrderCreateState extends State
{
    public OrderCreateState(int owner_order_status, State next_state) {
        super(owner_order_status, next_state);
    }

    @Override
    void diaplay() {
        System.out.println("订单创建");
    }
}
/**
 * 订单支付
 */
public class OrderPayState extends State {

    public OrderPayState(int owner_order_status, State next_state) {
        super(owner_order_status, next_state);
    }

    @Override
    void diaplay() {
        System.out.println("订单支付");
    }
}
/**
 * 订单完成
 */
public class OrderSuccessState extends State{
    public OrderSuccessState(int owner_order_status, State next_state) {
        super(owner_order_status, next_state);
    }

    @Override
    void diaplay() {
        System.out.println("订单完成");
    }
}
/**
 * 订单状态关闭
 */
public class OrderCloseState extends State{
    public OrderCloseState(int owner_order_status, State next_state) {
        super(owner_order_status, next_state);
    }

    @Override
    void diaplay() {
        System.out.println("订单状态关闭");
    }
}
/**
 * 订单
 */
public class Order {
    private State orderState;
    public Order(){
        orderState = new OrderCreateState(1,
                new OrderPayState(2,
                        new OrderSuccessState(3,
                                new OrderPayState(4,null))));
    }

    public State getOrderState() {
        return orderState;
    }
}
public class Main {
    public static void main(String[] args) {
        Order order = new Order();
        State orderState = order.getOrderState();
        orderState.handler(1);//订单创建
        orderState.handler(2);//订单支付
        orderState.handler(3);//订单完成
        orderState.handler(4);//订单关闭
        orderState.handler(5);//订单未定义
    }
}

结果:

订单创建
订单支付
订单完成
订单支付
当前订单状态未定义

 

源码地址:https://github.com/qjm201000/design_pattern_state.git

 

posted @ 2018-12-13 10:19  qjm201000  阅读(288)  评论(0编辑  收藏  举报