码迷,mamicode.com
首页 > 其他好文 > 详细

状态模式

时间:2021-04-19 16:06:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:color   nbsp   取出   read   private   else   读取   rri   set   

public class StatePattern {


    public static void main(String[] args) {
        Context context=new Context();
        for (int i = 0; i < 10; i++) {
            context.handle();
        }
    }
}
public class Context {


    public boolean isPrveReadPGRead() {
        return prveReadPGRead;
    }

    public void setPrveReadPGRead(boolean prveReadPGRead) {
        this.prveReadPGRead = prveReadPGRead;
    }

    private boolean prveReadPGRead;

    private State state;

    //定义环境类的初始状态
    public Context() {
        this.state = new ReadPGState();
    }

    //设置新状态
    public void setState(State state) {
        this.state = state;
    }

    //读取状态
    public State getState() {
        return (state);
    }

    //对请求做处理
    public void handle() {
        state.handle(this);
    }
}
public abstract class State {
    public abstract void handle(Context context);
}
public class ReadPGState extends State {
    @Override
    public void handle(Context context) {
        //读取pg数据
        System.out.println("都PG数据");

        System.out.println("发送mq");

        context.setPrveReadPGRead(true);
        context.setState(new ReadQueueState());
    }
}
public class ReadQueueState extends State {
    @Override
    public void handle(Context context) {
        //上一次执行是否为pg
        if (context.isPrveReadPGRead()){
            //map中是否包含Lsn

            boolean flag=true;
            if (flag){
                //压入
                System.out.println("map读取");
                context.setPrveReadPGRead(false);
                flag=false;
            }else {
                context.setState(new ReadPGState());
            }

        }else {
            boolean flag=true;
            //队列未满
            if (flag){
                //全部取出执行
                System.out.println("连续map读取");
                context.setPrveReadPGRead(false);
                flag=false;
            }else {
                context.setState(new ReadPGState());
            }
        }
    }
}

 

状态模式

标签:color   nbsp   取出   read   private   else   读取   rri   set   

原文地址:https://www.cnblogs.com/zhuyapeng/p/14671248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!