Design Patterns (17) - State Patterns

state mode

1. Definition

        Allows an object to change its behavior when its internal state changes. The object appears to modify its class.

2. Sample code

        The code demonstrates the corresponding handling of voting behavior.

  

/* Encapsulates the relevant behavior of a voting state */
public interface VoteState{
   / / Process the behavior corresponding to the state
   public void vote(String user,String voteItem,VoteManager voteManager);
}

/* Processing of normal voting status */
public class NormalVoteState implements VoteState{
  public void vote(String user,String voteItem,VoteManager voteManager){
       // record in the voting record
       voteManager.getMapVote().put(user,voteItem);
       System.out.println("Congratulations on your successful vote");
   }
}

/* Processing corresponding to duplicate voting status */
public class RepeatVoteState implements VoteState{
   public void vote(String user,String voteItem,VoteManager voteManager){
       //Repeated voting will not be processed for the time being
       System.out.println("Please do not vote again");
   }
}

/* Processing corresponding to multiple voting status*/
public class SpiteVoteState implements VoteState{
   public void vote(String user,String voteItem,VoteManager voteManager){
       //Cancel the user's voting qualification and cancel the voting record
       String s = voteManager.getMapVote().get(user);
       if(s != null){
          voteManager.getMapVote().remove(user);
       }
       System.out.println("Too many votes, disqualified from voting");
   }
}

/* Cancel the processing corresponding to the use state */
public class SpiteVoteState implements VoteState{
   public void vote(String user,String voteItem,VoteManager voteManager){
       System.out.println("Cannot use this system");
   }
}

   

/*vote management*/
public class VoteManager{
   //hold the state handler object
   private VoteState state = null;
   //Record the result of the user's vote, record the options of the user's vote
   private Map<String,String> mapVote = new HashMap<String,String>();
   //Record the number of times the user voted
   private Map<String,String> mapVoteCount = new HashMap<String,String>();
   public Map<String,String> getMapVote(){
       return mapVote;
   }
   // handle voting logic
   public void vote(String user,String voteItem){
      //Record the number of user votes
      Integer oldVoteCount = mapVoteCount.get(user);
      if(oldVoteCount == null){
          oldVoteCount = 0;
      }
      oldVoteCount += 1;
      mapVoteCount.put(user,oldVoteCount);
      // Determine user voting status
      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);
   }
}

   

/* Client calls voting */
public class Client{
    public static void main(String agr[]){
        VoteManager vm = new VoteManager();
        for(int i=0;i<0;i++){
           vm.vote("u1","A");
        }
    }
}

3. Practical application

      The state mode can simplify the control logic, but it also has an obvious disadvantage. One state corresponds to one processing class, which will make the program introduce too many state classes, which will make the program cluttered.

 

The essence of the state pattern: separation and selection of behavior based on state

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326387171&siteId=291194637