一、前言
允许一个对象的内部状态改变时改变它的行为。对象看起来似乎修改了它的类。
二、结构图
三、应用场景
◆ 在Context 中进行状态的维护和转换
public interface VoteState {
/**
* 处理状态对应的行为
* @param user 投票人
* @param voteItem 投票项
* @param manager 投票上下文,用来实现状态对应的功能处理的时候,可以回调上下文的数据
*/
public void vote(String user, String voteItem, VoteManager manager);
}
public class NormalVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
manager.getMapVote().put(user, voteItem);
System.out.println("恭喜你投票成功!");
}
}
public class RepeatVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("请不要重复投票!");
}
}
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("你有恶意刷票行为,取消投票资格。");
}
}
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("进入黑名单,将禁止登陆和使用本系统。");
}
}
public class 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
* @return 记录用户投票结果的Map
*/
public Map<String, String> getMapVote() {
return mapVote;
}
/**
* 投票
* @param user 投票人
* @param voteItem 投票的选项
*/
public void vote(String user, String voteItem){
Integer oldVoteCount = mapVoteCount.get(user);
if(oldVoteCount == null){
oldVoteCount = 0;
}
oldVoteCount++;
mapVoteCount.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 class Client {
public static void main(String[] args) {
VoteManager manager = new VoteManager();
for(int i = 0; i < 8; i++){
manager.vote("JOE", "A");
}
}
}
#########################################################################################################
◆ 在状态处理类中进行状态的维护和转换
public interface VoteState {
public void vote(String user, String voteItem, VoteManager manager);
}
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());
}
}
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());
}
}
}
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());
}
}
}
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("你进入了黑名单。");
}
}
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);
}
}
public class Client {
public static void main(String[] args) {
VoteManager manager = new VoteManager();
for(int i = 0; i < 8; i++){
manager.vote("JOE", "A");
}
}
}