Eighth chapter, State mode 1. Definition
The behavior in state mode is determined by the State, with different behaviors in different states. When an object's internal state changes and agrees to change its behavior, the object looks like it has changed its class.
2. Usage Scenarios
1. The behavior of an object depends on its state, and it must change its behavior based on its state at execution time.
2. The code includes a number of conditional statements related to the state of the object, such as a large number of multi-branch statements in an operation. And these branches depend on the state of the object.
3. Simple implementation
Realize the effect: first of all, the state of the TV is divided into power-on and shutdown state, the power-on can be achieved through the remote control channel and adjust the volume, but when the shutdown, these operations will fail.
The first is the common implementation method:
Public classTvcontroller {//Boot status PrivateFinalStatic intpower_on =1;//Shutdown status PrivateFinalStatic intPower_off =2;//default state Private intMstate = Power_off; Public void PowerOn(){if(Mstate ==power_off) {System. out. println ("The TV is on."); } mstate = power_on; } Public void PowerOff(){if(Mstate ==power_on) {System. out. println ("The TV is off."); } mstate = Power_off; } Public void Nextchannel(){if(Mstate ==power_on) {System. out. println ("Next channel"); }Else{System. out. println ("No Boot"); } } Public void Prevchannel(){if(Mstate ==power_on) {System. out. println ("Previous channel"); }Else{System. out. println ("No Boot"); } } Public void Turnup(){if(Mstate ==power_on) {System. out. println ("Increase volume"); }Else{System. out. println ("No Boot"); } } Public void turndown(){if(Mstate ==power_on) {System. out. println ("Volume Down"); }Else{System. out. println ("No Boot"); } }}
Can see that each execution is manipulated by inferring the current state. Part of the code is repeated. If States and features are added, it becomes increasingly difficult to maintain. The state mode can be used at this time. For example, the following:
Operation of the TV
/** * 电视状态接口,定义了电视的操作函数 * * */publicinterface TVState { publicvoidnextChannel(); publicvoidprevChannel(); publicvoidturnUp(); publicvoidturnDown();}
Shutdown status
/** * * Shutdown status, Operation no results * * * / Public class poweroffstate implements tvstate{ @Override Public void Nextchannel() { }@Override Public void Prevchannel() { }@Override Public void Turnup() { }@Override Public void turndown() { }}
Boot status
/** * * Boot status. Operation valid * * */ Public class poweronstate implements tvstate{ @Override Public void Nextchannel() {System.out.println ("Next channel"); }@Override Public void Prevchannel() {System.out.println ("Previous channel"); }@Override Public void Turnup() {System.out.println ("Increase volume"); }@Override Public void turndown() {System.out.println ("Volume Down"); }}
Power Operation Interface
/** * 电源操作接口 * * */publicinterface PowerController { publicvoidpowerOn(); publicvoidpowerOff();}
TV Remote Control
/** * TV Remote CONTROL * * * / Public class Tvcontroller implements Powercontroller{Tvstate mtvstate; Public void settvstate(Tvstate mtvstate) { This. mtvstate = mtvstate; }@Override Public void PowerOn() {Settvstate (NewPoweronstate ()); System.out.println ("Boot Up"); }@Override Public void PowerOff() {Settvstate (NewPoweroffstate ()); System.out.println ("Off the computer. "); } Public void Nextchannel() {Mtvstate.nextchannel (); } Public void Prevchannel() {Mtvstate.prevchannel (); } Public void Turnup() {mtvstate.turnup (); } Public void turndown() {Mtvstate.turndown (); }}
Call:
publicclass Client { publicstaticvoidmain(String[] args) { new TVController(); //设置开机状态 tvController.powerOn(); //下一频道 tvController.nextChannel(); //调高音量 tvController.turnUp(); //关机 tvController.powerOff(); //调低音量,此时不会生效 tvController.turnDown(); }}
Results
开机了下一频道调高音量关机了
Can see. The state pattern encapsulates these behaviors into a State class. These functions are forwarded to the state object at the time of operation, and different states have different implementations, except for repeating the If-else statement, which is the essence of the state pattern.
4. Differences from the Strategy model
The structure of the state mode and the strategy pattern is almost the same as the twins. But their eyes and nature are not the same.
The behavior of the state mode is parallel and not replaceable. The behavior of the policy mode is independent and interchangeable with each other. State mode, usually a change in the state of self-control. The policy mode, however, is what policies are used by the external designations.
5.Android use in combat
1. Log in to the system, depending on whether the user is logged in. Infers how events are handled.
2.wi-fi management, in different states, the WiFi scanning request processing varies.
6. Summary 1. Strengths
Putting all behaviors related to a particular state into a state object provides a better way to organize code that is relevant to a particular state. The tedious state inference is transformed into a State class family with clear structure. Scalability and maintainability are ensured at the same time to avoid code bloat.
2. Disadvantages
The use of State mode is bound to add the number of system classes and objects.
"Android source code design mode analysis and actual combat" reading notes (eight)