Article Directory
State mode
Overview
- When the internal state of an object is allowed to change its behavior, the object looks like it has changed its class.
- The structure of state mode and strategy mode is similar, but the problems they solve are different.
- In the state mode, the class has a state, and the modification of the state will change the behavior of the entire class.
- The strategy mode has no state, and the choice of strategy is determined by the client .
Character introduction
- Environmental role: Define the interface required by the client, and is responsible for the switching of specific states.
- The abstract state class defines the state method.
- The concrete state class inherits from the abstract state class and implements its concrete methods.
Basic realization
Traditional mode implementation
public class TVController {
//开机状态
private final static int POWER_ON = 1;
//关机状态
private final static int POWER_OFF = 2;
private int mState = POWER_OFF;
public void powerOn() {
mState = POWER_ON;
if (mState == POWER_ON) {
System.out.println("开机");
}
}
public void powerOff() {
mState = POWER_OFF;
if (mState == POWER_OFF) {
System.out.println("关机");
}
}
public void nextChannel() {
if (mState == POWER_ON) {
System.out.println("下一个频道");
} else {
System.out.println("还没有开机呢");
}
}
public void prevChannel() {
if (mState == POWER_ON) {
System.out.println("上一个频道");
} else {
System.out.println("还没有开机呢");
}
}
public void turnUp() {
if (mState == POWER_ON) {
System.out.println("调高音量");
} else {
System.out.println("还没有开机呢");
}
}
public void turnDown() {
if (mState == POWER_ON) {
System.out.println("降低音量");
} else {
System.out.println("还没有开机呢");
}
}
}
public class Demo2 {
public static void main(String[] args) {
TVController controller = new TVController();
controller.powerOn();
controller.nextChannel();
controller.turnDown();
controller.powerOff();
controller.nextChannel();
controller.turnDown();
}
}
开机
下一个频道
降低音量
关机
还没有开机呢
还没有开机呢
State mode implementation
Abstract state class
public interface TVState {
void nextChannel();
void prevChannel();
void turnUp();
void turnDown();
}
Specific status class
//开机状态
public class PowerOnState implements TVState {
@Override
public void nextChannel() {
System.out.println("下一个频道");
}
@Override
public void prevChannel() {
System.out.println("上一个频道");
}
@Override
public void turnUp() {
System.out.println("调高音量");
}
@Override
public void turnDown() {
System.out.println("降低音量");
}
}
//关机状态
public class PowerOffState implements TVState {
@Override
public void nextChannel() {
System.out.println("还没有开机呢");
}
@Override
public void prevChannel() {
System.out.println("还没有开机呢");
}
@Override
public void turnUp() {
System.out.println("还没有开机呢");
}
@Override
public void turnDown() {
System.out.println("还没有开机呢");
}
}
Environmental role
public interface PowerController {
void powerOn();
void powerOff();
}
public class TVController implements PowerController {
private TVState mTVState;
public void setTVState(TVState state) {
mTVState = state;
}
@Override
public void powerOn() {
setTVState(new PowerOnState());
System.out.println("开机");
}
@Override
public void powerOff() {
setTVState(new PowerOffState());
System.out.println("关机");
}
public void nextChannel() {
mTVState.nextChannel();
}
public void prevChannel() {
mTVState.prevChannel();
}
public void turnUp() {
mTVState.turnUp();
}
public void turnDown() {
mTVState.turnDown();
}
}
use
public class Demo {
public static void main(String[] args) {
TVController tvController = new TVController();
tvController.powerOn();
tvController.nextChannel();
tvController.turnUp();
tvController.powerOff();
tvController.nextChannel();
}
}
开机
下一个频道
调高音量
关机
还没有开机呢