I Introduction to status mode
Here is the reference
The behavior in the state mode is determined by the state, and there are different behaviors in different states. The structure of state mode and strategy mode is almost the same, but their purpose and essence are completely different. The behavior of state mode is parallel and irreplaceable, and the behavior of policy mode is independent and replaceable. In a word, state patterns wrap the behavior of objects in different state objects, and each state object has a common abstract base class. The intention of state mode is to make an object change its behavior when its internal state changes.
II Definition of state mode
When the internal state of an object changes, it is allowed to change its behavior. The object looks like it has changed its class.
III Usage scenario of state mode
1. The behavior of an object depends on its state, and it must change its behavior according to the state at runtime.
2. The code contains a large number of conditional statements related to status.
IV Examples
Take the remote control as an example to demonstrate the implementation of state mode. The remote control has two states: power on and power off. The channel can be switched and the volume can be adjusted through the remote control. But it cannot be operated in shutdown state.
public class TvController { private static final String TAG = "TvController"; //Power on status private final static int POWER_ON = 1; //Shutdown status private final static int POWER_OFF = 2; //current state private int mState = POWER_OFF; public void powerOn() { if (mState == POWER_OFF) { mState = POWER_ON; Log.d(TAG, "powerOn: It's on"); } } public void powerOff() { if (mState == POWER_ON) { mState = POWER_OFF; Log.d(TAG, "powerOff: It's off"); } } public void nextChannel() { if (mState == POWER_ON) { Log.d(TAG, "nextChannel: Next channel"); } else { Log.d(TAG, "nextChannel: No power on"); } } public void prevChannel() { if (mState == POWER_ON) { Log.d(TAG, "prevChannel: Previous channel"); } else { Log.d(TAG, "prevChannel: No power on"); } } public void turnUp() { if (mState == POWER_ON) { Log.d(TAG, "prevChannel: volume up "); } else { Log.d(TAG, "prevChannel: No power on"); } } public void turnDown() { if (mState == POWER_ON) { Log.d(TAG, "prevChannel: Volume down "); } else { Log.d(TAG, "prevChannel: No power on"); } } }
As a result, each function requires conditional judgment, and the code is repeated. If there are more functions, it will become more and more difficult to maintain.
State mode is to solve such problems.
Adjustment:
public interface TvState { void nextChannel(); void prevChannel(); void turnUp(); void turnDown(); }
public class PowerOffState implements TvState { @Override public void nextChannel() { } @Override public void prevChannel() { } @Override public void turnUp() { } @Override public void turnDown() { } }
public class PowerOnState implements TvState { private static final String TAG = "PowerOnState"; @Override public void nextChannel() { Log.d(TAG, "nextChannel: Next channel"); } @Override public void prevChannel() { Log.d(TAG, "prevChannel: Previous channel"); } @Override public void turnUp() { Log.d(TAG, "turnUp: volume up "); } @Override public void turnDown() { Log.d(TAG, "turnDown: Volume down "); } }
public interface PowerController { void powerOn(); void powerOff(); }
public class TvController implements PowerController { private static final String TAG = "TvController"; private TvState tvState; private void setTvState(TvState tvState) { this.tvState = tvState; } @Override public void powerOn() { setTvState(new PowerOnState()); Log.d(TAG, "powerOn: It's on"); } @Override public void powerOff() { setTvState(new PowerOffState()); Log.d(TAG, "powerOff: It's off"); } public void nextChannel() { tvState.nextChannel(); } public void prevChannel() { tvState.prevChannel(); } public void turnUp() { tvState.turnUp(); } public void turnDown() { tvState.turnDown(); } }
TvController tvController = new TvController(); tvController.powerOn(); tvController.nextChannel(); tvController.turnDown(); tvController.powerOff();
V summary
advantage:
State mode puts all the behaviors related to a specific state into a state object. It provides a better way to organize the code related to a specific state, convert the cumbersome state judgment into a state class with clear structure, and ensure the scalability and maintainability while avoiding the expansion of code.
Disadvantages:
The use of state mode will inevitably increase the number of system classes and objects.