标签:
状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模式。
状态模式允许一个对象在其内部状态改变的时候改变其行为,这个对象看上去就像是改变了它的类一样。
状态模式所涉及到的角色有:
1、环境(Context)角色,也叫上下文:定义客户端所感兴趣的接口,并且保留一个具体状态类的实例。这个具体状态类的实例给出此环境对象的现有状态。
2、抽象状态(State)角色:定义一个接口,用以封装环境(Context)对象的一个特定的状态所对应的行为。
3、具体状态(ConcreteState)角色:每一个具体状态类都实现了环境(Context)的一个状态所对应的行为。
例子:
publicclass Context { //持有一个State类型的对象实例private State state; publicvoid setState(State state) { this.state = state; } /** * 用户感兴趣的接口方法 */publicvoid request(String sampleParameter) { //转调state来处理 state.handle(sampleParameter); } }
publicinterface State { /** * 状态对应的处理 */publicvoid handle(String sampleParameter); }
publicclass ConcreteStateA implements State { @Override publicvoid handle(String sampleParameter) { System.out.println("ConcreteStateA handle :" + sampleParameter); } }
publicclass ConcreteStateB implements State { @Override publicvoid handle(String sampleParameter) { System.out.println("ConcreteStateB handle :" + sampleParameter); } }
publicclass Client { publicstaticvoid main(String[] args){ //创建状态 State state = new ConcreteStateB(); //创建环境 Context context = new Context(); //将状态设置到环境中
context.setState(state); //请求 context.request("test"); } }
系统的结构图如下所示:
publicinterface VoteState { /** * 处理状态对应的行为 * @param user 投票人 * @param voteItem 投票项 * @param voteManager 投票上下文,用来在实现状态对应的功能处理的时候, * 可以回调上下文的数据 */publicvoid vote(String user,String voteItem,VoteManager voteManager); }
publicclass NormalVoteState implements VoteState { @Override publicvoid vote(String user, String voteItem, VoteManager voteManager) { //正常投票,记录到投票记录中
voteManager.getMapVote().put(user, voteItem); System.out.println("恭喜投票成功"); } }
publicclass RepeatVoteState implements VoteState { @Override publicvoid vote(String user, String voteItem, VoteManager voteManager) { //重复投票,暂时不做处理 System.out.println("请不要重复投票"); } }
publicclass SpiteVoteState implements VoteState { @Override publicvoid vote(String user, String voteItem, VoteManager voteManager) { // 恶意投票,取消用户的投票资格,并取消投票记录 String str = voteManager.getMapVote().get(user); if(str != null){ voteManager.getMapVote().remove(user); } System.out.println("你有恶意刷屏行为,取消投票资格"); } }
publicclass BlackVoteState implements VoteState { @Override publicvoid vote(String user, String voteItem, VoteManager voteManager) { //记录黑名单中,禁止登录系统 System.out.println("进入黑名单,将禁止登录和使用本系统"); } }
publicclass VoteManager { //持有状体处理对象
private VoteState state = null; //记录用户投票的结果,Map<String,String>对应Map<用户名称,投票的选项>
private Map<String,String> mapVote = new HashMap<String,String>(); //记录用户投票次数,Map<String,Integer>对应Map<用户名称,投票的次数>
private Map<String,Integer> mapVoteCount = new HashMap<String,Integer>(); /** * 获取用户投票结果的Map */public Map<String, String> getMapVote() { return mapVote; } /** * 投票 * @param user 投票人 * @param voteItem 投票的选项 */publicvoid vote(String user,String voteItem){ //1.为该用户增加投票次数 //从记录中取出该用户已有的投票次数 Integer oldVoteCount = mapVoteCount.get(user); if(oldVoteCount == null){ oldVoteCount = 0; } oldVoteCount += 1; mapVoteCount.put(user, oldVoteCount); //2.判断该用户的投票类型,就相当于判断对应的状态 //到底是正常投票、重复投票、恶意投票还是上黑名单的状态
if(oldVoteCount == 1){ state = new NormalVoteState(); } elseif(oldVoteCount > 1 && oldVoteCount < 5){ state = new RepeatVoteState(); } elseif(oldVoteCount >= 5 && oldVoteCount <8){ state = new SpiteVoteState(); } elseif(oldVoteCount > 8){ state = new BlackVoteState(); } //然后转调状态对象来进行相应的操作 state.vote(user, voteItem, this); } }
publicclass Client { publicstaticvoid main(String[] args) { VoteManager vm = new VoteManager(); for(int i=0;i<9;i++){ vm.vote("u1","A"); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/wudiyong22/article/details/47444099