其实状态模式和策略模式在UML结构上是一样的,只是侧重点不同
State接口
package com.mjt.learning.pattern.state.demo1;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 3:05 PM<br>
* Desc: 状态模式下状态接口<br>
*/
public interface State {
void run();
}
BadState实现
package com.mjt.learning.pattern.state.demo1;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 3:08 PM<br>
* Desc: <br>
*/
public class BadState implements State {
@Override
public void run() {
System.out.println("今天马拉松用时4小时!");
}
}
NormalState实现
package com.mjt.learning.pattern.state.demo1;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 3:06 PM<br>
* Desc: <br>
*/
public class NormalState implements State{
@Override
public void run() {
System.out.println("今天马拉松用时5小时!");
}
}
GoodState实现
package com.mjt.learning.pattern.state.demo1;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 3:08 PM<br>
* Desc: <br>
*/
public class GoodState implements State {
@Override
public void run() {
System.out.println("今天马拉松用时3小时!");
}
}
Runner 相当于Context
package com.mjt.learning.pattern.state.demo1;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 3:08 PM<br>
* Desc: <br>
*/
public class Runner {
private State state = new NormalState();
public void setState(State state) {
this.state = state;
}
public void run() {
state.run();
}
public static void main(String[] args) {
Runner runner =new Runner();
runner.run();//正常状态
runner.setState(new BadState());
runner.run();
runner.setState(new GoodState());
runner.run();
}
}
进阶
投票问题
VoteState接口
package com.mjt.learning.pattern.state.demo2;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:26 PM<br>
* Desc: <br>
*/
public interface VoteState {
void vote(String user,String voteItem,VoteManager vm);
}
NormalVoteState 正常投票状态
package com.mjt.learning.pattern.state.demo2;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:28 PM<br>
* Desc: <br>
*/
public class NormalVoteState implements VoteState{
@Override
public void vote(String user, String voteItem, VoteManager vm) {
vm.getVoteResults().put(user,voteItem);
System.out.println("恭喜投票成功!");
}
}
RepeatVoteState重复投票状态
package com.mjt.learning.pattern.state.demo2;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:39 PM<br>
* Desc: <br>
*/
public class RepeatVoteState implements VoteState{
@Override
public void vote(String user, String voteItem, VoteManager vm) {
System.out.println("请不要重复投票!");
}
}
SpiteVoteState 恶意刷票状态
package com.mjt.learning.pattern.state.demo2;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:39 PM<br>
* Desc: <br>
*/
public class SpiteVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager vm) {
String s = vm.getVoteResults().get(user);
if (s!=null){
vm.getVoteResults().remove(user);
}
System.out.println("您有恶意刷票行为,取消投票结果!");
}
}
BlackVoteState 拉入黑名单状态
package com.mjt.learning.pattern.state.demo2;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:40 PM<br>
* Desc: <br>
*/
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager vm) {
System.out.println("进入黑名单,将禁止登陆和使用系统!");
}
}
VoteManager 投票管理
package com.mjt.learning.pattern.state.demo2;
import java.util.HashMap;
import java.util.Map;
/**
* Copyright:Learning
* Author: liyang <br>
* Date:2019/3/25 5:27 PM<br>
* Desc: <br>
*/
class VoteManager {
//持有状态处理对象
private VoteState state = null;
//记录用户投票结果 <用户姓名,投票的选项>
private Map<String, String> voteResults = new HashMap<>();
//记录用户投票次数<用户姓名,投票的次数>
private Map<String, Integer> voteCounts = new HashMap<>();
public Map<String, String> getVoteResults() {
return voteResults;
}
public void vote(String user, String voteItem) {
Integer oldVoteCount = voteCounts.get(user);
if (oldVoteCount == null) {
oldVoteCount = 0;
}
oldVoteCount += 1;
voteCounts.put(user, oldVoteCount);
if (oldVoteCount == 1) {
state = new NormalVoteState();
} else if (oldVoteCount > 1 && oldVoteCount <= 5) {
state = new RepeatVoteState();
} else if (oldVoteCount > 5 && oldVoteCount < 8) {
state = new SpiteVoteState();
} else if (oldVoteCount >= 8) {
state = new BlackVoteState();
}
state.vote(user, voteItem, this);
}
public static void main(String[] args) {
VoteManager vm=new VoteManager();
for (int i = 0; i < 9; i++) {
vm.vote("hanfei","A");
}
}
}
输出结果:
为恭喜投票成功!
请不要重复投票!
请不要重复投票!
请不要重复投票!
请不要重复投票!
您有恶意刷票行为,取消投票结果!
您有恶意刷票行为,取消投票结果!
进入黑名单,将禁止登陆和使用系统!
进入黑名单,将禁止登陆和使用系统!
网友评论