一句话总结:
状态设计模式:将各种状态,分离出来,避免以后状态添加,修改,影响到客户端代码。
代码结构:
状态类的接口State;具体的状态类StateA,StateB,StateC;状态类的管理类StateManager;客户端Client;
具体代码:
现在引用网上写的一个好的例子。
状态类的接口State:
package state;具体的状态类StateA,StateB,StateC:
public interface VoteState {
void vote(String user, String voteItem, VoteManager manager);
}
package state;
public class NormalVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
manager.getVoteOption().put(user, voteItem);
System.out.println("正常投票");
}
}
package state;
public class RepeatVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("重复投票");
}
}
package state;
public class EvilVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
if (null != manager.getVoteOption().get(user)) {
manager.getVoteOption().remove(user);
}
System.out.println("恶意投票");
}
}
package state;状态类的管理类StateManager:
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("加入黑名单");
}
}
状态管理类特别关键,在各种状态转换规则确定的时候,可以将转换标准和转换算法都放在这里面。如果转换规则不确定就要分解到各个状态中了。
package state;
import java.util.HashMap;
import java.util.Map;
public class VoteManager {
private VoteState state;
private Map<String, String> voteOption = new HashMap<>();
private Map<String, Integer> voteCount = new HashMap<>();
public Map<String, Integer> getVoteCount() {
return voteCount;
}
public Map<String, String> getVoteOption() {
return voteOption;
}
public void vote(String user, String voteOption){
int count = 0;
if (null != voteCount.get(user)) {
count = voteCount.get(user);
}
count++;
voteCount.put(user, count);
if (1 == count) {
state = new NormalVoteState();
}else if(count < 5 && count > 1){
state = new RepeatVoteState();
}else if(count < 8 && count >= 5){
state = new EvilVoteState();
}else if(count >= 8){
state = new BlackVoteState();
}
state.vote(user, voteOption, this);
}
}
客户端Client:
package state;
public class Main {
public static void main(String[] args) {
VoteManager manager = new VoteManager();
for (int i = 0; i < 10; i++) {
manager.vote("zhangsan", "fahd");
}
}
}
结果是:
正常投票
重复投票
重复投票
重复投票
恶意投票
恶意投票
恶意投票
加入黑名单
加入黑名单
加入黑名单
----------------------------------------------- 假如转换规则不确定:状态类的接口State:
package state;具体的状态类StateA,StateB,StateC:
public interface VoteState {
void vote(String user, String voteItem, VoteManager manager);
}
算法分解之后,尤其需要注意在状态转换的时候也要调用投票方法。
package state2;
public class NormalVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
manager.getVoteCount().put(user, 1);
manager.getVoteOption().put(user, voteItem);
manager.getVoteStates().put(user, new RepeatVoteState());
System.out.println("正常投票");
}
}
package state2;
public class RepeatVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
int count = manager.getVoteCount().get(user) + 1;
if (count < 5) {
manager.getVoteCount().put(user, count);
System.out.println("重复投票");
}else{
manager.getVoteStates().put(user, new EvilVoteState());
manager.getVoteStates().get(user).vote(user, voteItem, manager);
}
}
}
package state2;
public class EvilVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
int count = manager.getVoteCount().get(user) + 1;
if (count < 8) {
manager.getVoteCount().put(user, count);
if (null != manager.getVoteOption().get(user)) {
manager.getVoteOption().remove(user);
}
System.out.println("恶意投票");
}else{
manager.getVoteStates().put(user, new BlackVoteState());
manager.getVoteStates().get(user).vote(user, voteItem, manager);
}
}
}
package state2;
public class BlackVoteState implements VoteState {
@Override
public void vote(String user, String voteItem, VoteManager manager) {
System.out.println("加入黑名单");
}
}
状态类的管理类StateManager:
package state2;
import java.util.HashMap;
import java.util.Map;
public class VoteManager {
private Map<String, VoteState> voteStates = new HashMap<>();
private Map<String, String> voteOption = new HashMap<>();
private Map<String, Integer> voteCount = new HashMap<>();
public Map<String, Integer> getVoteCount() {
return voteCount;
}
public Map<String, String> getVoteOption() {
return voteOption;
}
public Map<String, VoteState> getVoteStates() {
return voteStates;
}
public void vote(String user, String voteOption){
VoteState voteState = voteStates.get(user);
if (null == voteState) {
voteState = new NormalVoteState();
voteStates.put(user, voteState);
}
voteState.vote(user, voteOption, this);
}
}
客户端Client:
<span style="font-family:Heiti SC Light;">package state;
public class Main {
public static void main(String[] args) {
VoteManager manager = new VoteManager();
for (int i = 0; i < 10; i++) {
manager.vote("zhangsan", "fahd");
}
}
}
</span>
结果是:
正常投票
重复投票
重复投票
重复投票
恶意投票
恶意投票
恶意投票
加入黑名单
加入黑名单
加入黑名单
适用场景:
优点:
1.状态决定行为,可以将状态分离出来,可以很好的单独管理
缺点:
1.状态类多了,就不好管理了
写的比较好的博客:
http://joe5456536.blog.163.com/blog/static/85374773201192252829941/