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 to allow its behavior to change, 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 at run time based on its state.
2. The code contains a number of conditional statements related to the state of the object, for example, an operation that contains a large number of multi-branch statements that 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 realize the channel switching and adjust the volume, but when the shutdown, these operations will be invalidated.
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"); } }}
As you can see, each execution is performed by judging the current state, and some of the code repeats, assuming that the state and function increases, it becomes more and more difficult to maintain. You can then use the state mode, as follows:
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
开机了下一频道调高音量关机了
As you can see, the state pattern encapsulates these behaviors into the state class, which forwards them to the state object at the time of operation, the different states have different implementations, and the if-else statements are removed, which is the essence of the state pattern.
4. Differences from the policy model
The state pattern is almost identical to the structure of the strategy pattern, like a twin brother. But they are different in their eyes and in nature. The behavior of the state mode is parallel and irreplaceable, and 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 the system, according to whether the user login, to determine how the event processing.
2.wi-fi management, in different states, the WiFi scanning request processing varies.
6. Summary 1. Advantages
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, transforming cumbersome state judgments into well-structured state-class families, and ensuring scalability and maintainability while avoiding code bloat.
2. Disadvantages
The use of State mode will inevitably increase the number of system classes and objects.
"Android source design mode analysis and actual combat" reading notes (eight)