class="java">
package pattern;
public interface VoteState {
public void vote(String user, String voteItem, VoteManager manager);
}
package pattern;
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("你进入了黑名单。");
}
}
package pattern;
public class NormalVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
manager.getMapVote().put(user, voteItem);
System.out.println("投票成功!");
manager.getMapState().put(user, new RepeatVoteState());
}
}
package pattern;
public class RepeatVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("请不要重复投票。");
if (manager.getMapVoteCount().get(user) >= 4) {
manager.getMapState().put(user, new SpiteVoteState());
}
}
}
package pattern;
public class SpiteVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
String s = manager.getMapVote().get(user);
if (s != null) {
manager.getMapVote().remove(user);
}
System.out.println("你有恶意刷票行为,取消投票资格。");
if (manager.getMapVoteCount().get(user) >= 7) {
manager.getMapState().put(user, new BlackVoteState());
}
}
}
package pattern;
import java.util.HashMap;
import java.util.Map;
public class VoteManager {
/**
* 记录当前每个用户对应的状态处理对象,每个用户当前的状态是不同的
* Map<String, VoteState>对应Map<用户名,当前对应的状态处理对象>
*/
private Map<String, VoteState> mapState = new HashMap<String, VoteState>();
/**
* 记录用户投票的结果,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
* @return 记录用户投票结果的Map
*/
public Map<String, String> getMapVote() {
return mapVote;
}
/**
* 获取记录当前每个用户对应的状态处理对象
* @return
*/
public Map<String, VoteState> getMapState() {
return mapState;
}
/**
* 获取记录每个用户对应的投票次数的Map
* @return
*/
public Map<String, Integer> getMapVoteCount() {
return mapVoteCount;
}
public void vote(String user, String voteItem){
Integer oldVoteCount = mapVoteCount.get(user);
if(oldVoteCount == null){
oldVoteCount = 0;
}
oldVoteCount++;
mapVoteCount.put(user, oldVoteCount);
VoteState state = mapState.get(user);
if(state == null){
state = new NormalVoteState();
}
state.vote(user, voteItem, this);
}
}
package pattern;
public class Client {
public static void main(String[] args) {
VoteManager manager = new VoteManager();
for (int i = 0; i < 8; i++) {
manager.vote("JOE", "A");
}
}
}
- 大小: 84.1 KB