设计模式——状态设计模式
来源:互联网 发布:java报表工具 开源 编辑:程序博客网 时间:2024/05/18 02:14
一句话总结:
状态设计模式:将各种状态,分离出来,避免以后状态添加,修改,影响到客户端代码。
代码结构:
状态类的接口State;具体的状态类StateA,StateB,StateC;状态类的管理类StateManager;客户端Client;
具体代码:
现在引用网上写的一个好的例子。
状态类的接口State:
package state;public interface VoteState {void vote(String user, String voteItem, VoteManager manager);}具体的状态类StateA,StateB,StateC:
package state;public class NormalVoteState implements VoteState {@Overridepublic void vote(String user, String voteItem, VoteManager manager) {manager.getVoteOption().put(user, voteItem);System.out.println("正常投票");}}
package state;public class RepeatVoteState implements VoteState {@Overridepublic void vote(String user, String voteItem, VoteManager manager) {System.out.println("重复投票");}}
package state;public class EvilVoteState implements VoteState {@Overridepublic void vote(String user, String voteItem, VoteManager manager) {if (null != manager.getVoteOption().get(user)) {manager.getVoteOption().remove(user);}System.out.println("恶意投票");}}
package state;public class BlackVoteState implements VoteState {@Overridepublic void vote(String user, String voteItem, VoteManager manager) {System.out.println("加入黑名单");}}状态类的管理类StateManager:
状态管理类特别关键,在各种状态转换规则确定的时候,可以将转换标准和转换算法都放在这里面。如果转换规则不确定就要分解到各个状态中了。
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;public interface VoteState {void vote(String user, String voteItem, VoteManager manager);}具体的状态类StateA,StateB,StateC:
算法分解之后,尤其需要注意在状态转换的时候也要调用投票方法。
package state2;public class NormalVoteState implements VoteState {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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/
http://blog.csdn.net/java_1111/article/details/8653418
http://www.2cto.com/kf/201212/180520.html
0 0
- 设计模式—状态模式
- 设计模式—状态模式
- 设计模式——状态设计模式
- 设计模式——状态模式(State)
- 设计模式——状态模式
- java设计模式——状态模式
- PHP设计模式——状态模式
- 设计模式之——状态模式
- 设计模式之——状态模式
- 设计模式(19)——状态模式
- 设计模式——状态模式
- C++设计模式——状态模式
- 设计模式——状态模式
- 设计模式——状态模式
- 设计模式——状态模式
- 设计模式——状态模式
- 设计模式<五>——状态模式
- PHP设计模式——状态模式
- linux ioctl 函数
- Div+Css实现图层固定,吊窗
- 【DP】UVA 624 CD 记录路径
- Linux系统调用--getrlimit()与setrlimit()函数详解
- 完全免费的pdf转换成word在线转换工具
- 设计模式——状态设计模式
- Android开发规范——命名
- 8.1 计算器的再改进
- [1298]活动选择 山东理工OJ
- 简单的四则运算!【练习】
- hdoj.1877 又一版 A+B 20140804
- BestCoder round#3 1001 && Hdu 4907
- Linear Algebra--Length and Dot Products of Vectors
- hdu1252