java设计模式-状态模式(State)

来源:互联网 发布:高分三号 预处理 软件 编辑:程序博客网 时间:2024/05/12 10:33

定义:允许一个对象的内部状态改变时改变它的行为。对象看起来似乎修改了它的类。

本质:根据状态来分离和选择行为。

状态:对象实例的属性值。

行为:对象的功能,大多对应到方法上。

状态模式的功能:分离状态的行为,通过维护状态的变化,来调用不同装态对应的不同功能。

状态和行为是相关联的,可以描述为:状态决定行为

状态模式和策略模式的结构师完全一样的,但它们的目的、实现、本质却完全不一样。状态模式的行为是平行的,不可相互替换的;而策略模式的行为是平等性的,是可以相互替换的

Context :环境,也称为上下文,用来定义客户感兴趣的接口,同时维护一个具体处理当前状态的实例对象。
State:状态接口,用来封装与上下文的一个特定状态所对应的行为。
ConcreteState:具体实现状态的处理类,每个实现一个跟上下文相关联的状态的具体处理。
 
状态处理对象的创建和销毁
① 当需要使用状态对象的时候创建,使用完后就销毁。
② 提前创建它们并永不销毁(单例)。
③ 采用延时加载 +  缓存的方式。就是当第一次使用的时候创建,使用完后并不销毁而是把它缓存起来,下一次使用时,从缓存中获取对象,并由缓存框架来销毁状态处理对象。
 

状态的维护和转换:维护状态的数据,给状态设置不同的状态值;状态的转换,指的是根据状态的变化来选择不同的状态处理对象。

有两个地方可以进行状态的维护和转换:

① 在Context 中。② 在状态的处理类中。

如何选择者两种方式:

● 如果状态转换的规则是一定的,一般不需要进行什么扩展规则,那么就适合在上下文中统一进行状态的维护。

● 如果状态的转换取决于前一个状态动态处理的结果,或者是依赖外部数据,为了增强灵活性,这种情况下,一般式在状态处理类中进行状态的维护。

◆ 在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");                    }          } }
原创粉丝点击