状态模式的定义
允许一个对象在其内部状态改变时改变它的行为,对象看起来是修改了它的类。
状态模式的本质
根据状态来分离和选择行为。
示例1
/**
* 封装与Context的一个特定状态相关的行为
*/
public interface State {
/**
* 状态对象的处理
* @param sampleParameter 示例参数,说明可以传入什么样的参数、传入几个由具体应用具体分析
*/
void handle(String sampleParameter);
}
/**
* 实现一个与Context的一个特定状态相关的行为
*/
public class ConcreteStateA implements State {
@Override
public void handle(String sampleParameter) {
//实现具体的处理
}
}
/**
* 实现一个与Context的一个特定状态相关的行为
*/
public class ConcreteStateB implements State {
@Override
public void handle(String sampleParameter) {
//实现具体的处理
}
}
/**
* 定义客户感兴趣的接口,通常会维护一个State类型的对象实例
*/
public class Context {
/**
* 持有一个State类型的对象实例
*/
private State state;
/**
* 用户感兴趣的接口方法
* @param sampleParameter
*/
public void request(String sampleParameter) {
//在处理中,会转调state来处理
state.handle(sampleParameter);
}
public void setState(State state) {
this.state = state;
}
}
示例2
/**
* 封装一个投票状态相关的行为
*/
public interface VoteState {
/**
* 处理状态对应的行为
* @param user 投票人
* @param voteItem 投票项
* @param voteManager 投票上下文,用来在实现状态对应的功能处理的时候,
* 可以回调上下文的数据。
*/
void vote(String user,String voteItem,VoteManager voteManager);
}
public class NormalVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager voteManager) {
voteManager.getMapVote().put(user,voteItem);
System.out.println("恭喜你投票成功");
}
}
public class RepeatVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager voteManager) {
//重复投票
//暂时不做处理
System.out.println("请不要重复投票");
}
}
public class SpiteVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager voteManager) {
//恶意投票
//取消用户的投票资格,并取消投票记录
String s = voteManager.getMapVote().get(user);
if (s != null) {
voteManager.getMapVote().remove(user);
}
System.out.println("你有恶意刷票行为,取消投票资格");
}
}
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager voteManager) {
//黑名单
//记入黑名单中
System.out.println("进入黑名单,将禁止登录和使用本系统");
}
}
import java.util.HashMap;
import java.util.Map;
/**
* 投票管理
*/
public class VoteManager {
/**
* 持有状态处理对象
*/
private VoteState state = null;
/**
* 记录用户投票的结果
*/
private Map<String, String> mapVote = new HashMap<>();
/**
* 记录用户投票次数
*/
private Map<String, Integer> mapVoteCount = new HashMap<>();
public Map<String, String> getMapVote() {
return mapVote;
}
/**
* 投票
* @param user 投票人
* @param voteItem 投票的选项
*/
public void vote(String user, String voteItem) {
//1.先为用户投票增加次数
Integer oldVoteCount = mapVoteCount.get(user);
if (null == oldVoteCount) {
oldVoteCount = 0;
}
oldVoteCount = oldVoteCount + 1;
mapVoteCount.put(user, oldVoteCount);
//2.判断该用户投票的类型
if (1 == oldVoteCount) {
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 TestClient {
public static void main(String[] args) {
VoteManager vm = new VoteManager();
for(int i = 0;i<8;i++) {
vm.vote("ul","A");
}
}
}