设计模式_21_状态模式

基本介绍

  1. 状态模式(State Pattern) :它主要用来解决对象在多种状态转换时,需要对外输出不同的行为的问题。状态和行为是一对应的,状态之间可以相互转换
  2. 当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类
    原理图:


    image.png

1)Context 类为环境角色,用于维护State实例,这个实例定义当前状态
2)State 是抽象状态角色,定义一-个接口封装与Context的一一个特点接口相关行为
3)ConcreteState 具体的状态角色,每个子类实现一一个 与Context的一个状态相关行为
例子:
需求:


image.png

实现:


image.png
public abstract class State {

    public abstract void deduceMoney();
    
    public abstract boolean raffle();
    
    public abstract void dispensePrize();

}

public class NoRaffleState extends State{
    
    Active active;
    
    public NoRaffleState(Active active) {
        super();
        this.active = active;
    }

    @Override
    public void deduceMoney() {
        // TODO Auto-generated method stub
        System.out.println("积分扣除50,可以开始抽奖。。。");
        active.setState(active.getCanRaffleState());
        
    }

    @Override
    public boolean raffle() {
        // TODO Auto-generated method stub
        System.out.println("现在不可以抽奖,需要先扣除积分!!!");
        return false;
    }

    @Override
    public void dispensePrize() {
        // TODO Auto-generated method stub
        System.out.println("现在不可以领奖,请先抽奖!!!");
    }

}

public class CanRaffleState extends State{
    
    Active active;

    public CanRaffleState(Active active) {
        super();
        this.active = active;
    }

    @Override
    public void deduceMoney() {
        // TODO Auto-generated method stub
        System.out.println("积分已经扣除,请开始抽奖。。。");
    }

    @Override
    public boolean raffle() {
        // TODO Auto-generated method stub
        
        Random random = new Random();
        int num = random.nextInt(9);
        if (num == 0) {
            System.out.println("恭喜您抽中奖品!!!");
            active.setState(active.getDispenseState());
            return true;
        }else{
            System.out.println("很遗憾,您未抽中奖品。。。");
            active.setState(active.getNoRaffleState());
            return false;
        }
    }

    @Override
    public void dispensePrize() {
        // TODO Auto-generated method stub
        System.out.println("请先抽取奖品!!!");
    }

}

public class DispenseState extends State{
    
    Active active;

    public DispenseState(Active active) {
        // TODO Auto-generated constructor stub
        this.active = active;
    }

    @Override
    public void deduceMoney() {
        // TODO Auto-generated method stub
        System.out.println("您有奖品可以领取!!!");
    }

    @Override
    public boolean raffle() {
        // TODO Auto-generated method stub
        System.out.println("您有奖品可以领取!!!");
        return false;
    }

    @Override
    public void dispensePrize() {
        // TODO Auto-generated method stub
        int num = active.getCount();
        if (num > 0 ) {
            System.out.println("您的奖品已经领取!!!");
            active.setState(active.getNoRaffleState()); 
            
            if (num == 1) {
                System.out.println("奖品已经发放完了,活动结束!!!");
                active.setState(active.getDispenseOutState());
            }
            
        }
    }

}

public class DispenseOutState extends State{
    
    Active active;

    public DispenseOutState(Active active) {
        super();
        this.active = active;
    }

    @Override
    public void deduceMoney() {
        // TODO Auto-generated method stub
        System.out.println("奖品已经发放完。。。");
    }

    @Override
    public boolean raffle() {
        // TODO Auto-generated method stub
        System.out.println("奖品已经发放完。。。");
        return false;
    }

    @Override
    public void dispensePrize() {
        // TODO Auto-generated method stub
        System.out.println("奖品已经发放完。。。");
    }

}

public class Active {
    
    State state;
    
    NoRaffleState noRaffleState = new NoRaffleState(this);
    CanRaffleState canRaffleState = new CanRaffleState(this);
    DispenseState dispenseState = new DispenseState(this);
    DispenseOutState dispenseOutState = new DispenseOutState(this);
    
    int count = 0;
    
    public Active(int count) {
        super();
        state = getNoRaffleState();
        this.count = count;
    }
    

    public boolean raffle() {
        // TODO Auto-generated method stub
        state.deduceMoney();
        if (state.raffle()) {
           state.dispensePrize();
           return true;
        }
        return false;
    }

    
    public int getCount(){
        int temp = count;
        count--;
        return temp;
    }   
    
    public void setState(State state) {
        this.state = state;
    }
    public State getState() {
        return state;
    }
    public NoRaffleState getNoRaffleState() {
        return noRaffleState;
    }
    public CanRaffleState getCanRaffleState() {
        return canRaffleState;
    }
    public DispenseState getDispenseState() {
        return dispenseState;
    }
    public DispenseOutState getDispenseOutState() {
        return dispenseOutState;
    }   
}

public class Client {

    public static void main(String[] args) throws InterruptedException {
        
        Active active = new Active(2);
        
        for(int i=0; i<30;i++){
            System.out.println("第"+(i+1)+"次抽奖:"+active.raffle());
        }
    }
}

状态模式的注意事项和细节

  1. 代码有很强的可读性。状态模式将每个状态的行为封装到对应的一个类中
  2. 方便维护。将容易产生问题的if-else语句删除了,如果把每个状态的行为都放到- -个类中,每次调用方法时都要判断当前是什么状态,不但会产出很多if-else语句,.而且容易出错
  3. 符合“开闭原则”。容易增删状态
  4. 会产生很多类。每个状态都要一个对应的类,当状态过多时会产生很多类,加大维护难度
  5. 当一个事件或者对象有很多种状态,状态之间会相互转换,对不同的状态要求有不同的行为的时候,可以考虑使用状态模式
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。