This article is from: Cao shenghuan blog column. Reprinted please indicate the source:Http://blog.csdn.net/csh624366188
State mode: allows an object to change its behavior when its internal state changes. The object seems to have modified its class. It seems that the status mode is very powerful-it can "modify its own class "! Let's take a look at his strength!
Applicable scenarios:
The State mode mainly solves the problem when the conditional expressions that control the status of an object are too complex. Transferring the State judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic. When an object's behavior depends on its state, and it must change its behavior according to its state at runtime, you can consider using the state mode.
Key points:
1. The rule mode and state mode are twins. They have the same class diagram, but their intentions are different. The rule mode successfully creates businesses around interchangeable algorithms. However, the State mode helps an object control its own behavior by changing its internal state.
2. Context delegates state-related operations to the current concrete State object for processing.
3. context can pass itself as a parameter to the status object that processes the request. This allows State objects to access context when necessary.
4. The context or concrete state class can determine which State is the successor and under which conditions the State is converted. That is to say, you can save the reference to the concrete state in the state and set the specific state as necessary to convert the state.
5. Generally, when the state transition is fixed, the state transition is suitable for storing in context. However, when the conversion is more dynamic, it is usually placed in a specific status class. (The status class holds the reference of context to convert the status)
Context class: maintains an instance of the concretestate subclass, which defines the current state.
State Class: abstract state class, which defines an interface to encapsulate a behavior related to a specific State of context.
Concretestatea, concretestateb class: a specific state class. each subclass implements a state-related Behavior of the context.
Specific implementation:
First define the interface userstate
1 public interface userstate {2 Public userservice = new userservice (); 3 // The status 0 indicates a common user, these statuses are mainly used for persistence to data 4 public final static int common_status = 0; 5 // If the status is 1, 6 public final static int for_verify_status = 1 indicates the user to be reviewed; 7 // If the status is 2, the VIP user 8 Public final static int vip_status = 2; 9 10 // submit information 11 Public void submit () throws exception; 12 13 // Audit Failed 14 public void rollback () throws exception; 15 16 // audit passed 17 Public void pass () throws exception; 18 19 public int getstatus ();
Classes representing various states
Unsubmitted user notsubmituser. Java
2 public class NotSubmitUser implements UserState { 3 public void pass() throws Exception { 4 throw new Exception("no support method"); 5 } 6 7 public void rollback() throws Exception { 8 throw new Exception("no support method"); 9 } 10 11 public void submit() throws Exception { 12 User user = userService.getUser(0); 13 user.setStauts(FOR_VERIFY_STATUS); 14 userService.update(user); 15 } 16 17 public int getStatus() { 18 return COMMON_STATUS; 19 } 20 }
User to be reviewed waitforverifyuser. Java
Java code
22 public class WaitForVerifyUser implements UserState { 23 24 public void pass() throws Exception { 25 User user = userService.getUser(0); 26 user.setStauts(VIP_STATUS); 27 userService.update(user); 28 } 29 30 public void rollback() throws Exception { 31 User user = userService.getUser(0); 32 user.setStauts(COMMON_STATUS); 33 userService.update(user); 34 } 35 36 public void submit() throws Exception { 37 throw new Exception("no support method"); 38 } 39 40 public int getStatus() { 41 return VIP_STATUS; 42 } 43 }
Approved user, VIP vipuser. Java
45 public class VipUser implements UserState { 46 47 public void pass() throws Exception { 48 throw new Exception("no support method"); 49 } 50 51 public void rollback() throws Exception { 52 User user = userService.getUser(0); 53 user.setStauts(COMMON_STATUS); 54 userService.update(user); 55 } 56 57 public void submit() throws Exception { 58 throw new Exception("no support method"); 59 } 60 61 public int getStatus() { 62 return FOR_VERIFY_STATUS; 63 } 64 }
Different userstates are returned based on the user status.
66 public class UserStateFactory { 67 static List list = null; 68 static { 69 list = new ArrayList(); 70 list.add(new NotSubmitUser()); 71 list.add(new VipUser()); 72 list.add(new WaitForVerifyUser()); 73 74 } 75 76 public static UserState getUserState(int status) { 77 for (UserState state : list) { 78 if (state.getStatus() == status) 79 return state; 80 } 81 return null; 82 } 83 }
Status mode operations are fixed, but the recipient classes are different. The principle of polymorphism depends not only on the method signature, but also on the recipient class of the operation.
This example is only applicable to the state mode scenario.
Advantages of status Mode:
1. excellent scalability-Assume that a user group is added: When the VIP user's credit reaches a certain level, it is upgraded to a permanent VIP. In this case, you only need to add another status class and do not need to modify the original code.
2. The code structure is clear and different errors occur. That is, the program is robust-except getuserstate (INT status) of userstatefactory, the logic of other methods is very simple and does not contain local variables. If the program does not need to be persisted to the database or getuserstate (INT status), this method is out of the Status mode content. The number of status mode classes is greatly increased.
Note: This example is only an application of the state mode. It is not a state mode but a specific one.