大话设计模式-状态模式的实践-投票

版权声明:Leo.All Rights Reserved. https://blog.csdn.net/qq_41113081/article/details/89479346

题目说明

在线投票
1.同一个用户只能投一票
2.如果一个用户反复投票,而且投票次数超过5次,判定为恶意投票,要取消该用户的投票资格,并取消他所投的票
3.若超过8次,将进入黑名单,禁止再登录和使用系统
*/

表示当前投票状态的抽象类

/**
 * @ClassName: State
 * @Author: Leo
 * @Description:
 * @Date: 2019/4/23 18:57
 */
public abstract class State {
    /**
     * 投票姓名
     */
    private String name;
    /**
     * 投票次数
     */
    private int count;
    /**
     * 是否还有投票资格
     */
    private boolean flag;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public abstract void vote(Context context);
}

三个具体的类 表示不同的投票者状态

/**
 * @ClassName: One
 * @Author: Leo
 * @Description:
 * @Date: 2019/4/23 18:54
 */
public class One extends State {
    @Override
    public void vote(Context context) {
        if (context.getState().getCount() < 5) {
            System.out.println("投票一次!");
            context.getState().setCount(context.getState().getCount() + 1);
        } else {
            Five five = new Five();
            five.setCount(5);
            context.setState(five);
            context.vote();
        }
    }
}
/**
 * @ClassName: Five
 * @Author: Leo
 * @Description:
 * @Date: 2019/4/23 18:54
 */
public class Five extends State {
    @Override
    public void vote(Context context) {
        if (context.getState().getCount() >= 5 && context.getState().getCount() < 8) {
            System.out.println("投票超过五次!取消该用户投票资格!");
            context.getState().setCount(context.getState().getCount() + 1);
        } else {
            Eight eight = new Eight();
            eight.setCount(8);
            context.setState(eight);
            context.vote();
        }
    }
}

/**
 * @ClassName: Eight
 * @Author: Leo
 * @Description:
 * @Date: 2019/4/23 18:55
 */
public class Eight extends State {
    @Override
    public void vote(Context context) {
        System.out.println("投票超过八次!拉黑! 禁止再登录和使用系统!");
    }
}

上下文类 管理不同的状态 对外提供接口

public class Context {
    private State state;

    public void vote() {
        state.vote(this);
    }

    public Context() {
    }

    public Context(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
}

测试类

public class Main {
    public static void main(String[] args) {
        Context context = new Context(new One());
        context.vote();
        context.vote();
        context.vote();
        context.vote();
        context.vote();
        context.vote();
        context.vote();
        context.vote();
        context.vote();
    }
}


猜你喜欢

转载自blog.csdn.net/qq_41113081/article/details/89479346
今日推荐