状态模式(State Pattern)是一种对象行为型模式,它允许一个对象在其内部状态改变时改变它的行为,使得对象看起来似乎改变了其类。状态模式可以有效地消除大量的条件语句,将复杂的状态判断逻辑转换为结构清晰的状态类族和简单的状态切换逻辑。
在Java语言中,实现状态模式通常需要使用接口和枚举类型,下面从多个角度分析状态模式在Java中的应用。
1. 接口与多态
面向接口编程是Java语言中的一种重要思想,状态模式中的状态类也是通过接口定义的。状态类实现状态接口,并实现状态接口中定义的方法,不同状态的实现方式不同,通过多态实现状态的切换。
例如,实现一个简单的电视机状态控制器,在开机、关机和播放状态之间切换。首先定义接口State:
```
public interface State {
void doAction();
}
```
定义状态类,实现State接口:
```
public class TVStartState implements State {
public void doAction() {
System.out.println("TV is turned ON");
}
}
public class TVStopState implements State {
public void doAction() {
System.out.println("TV is turned OFF");
}
}
public class TVPlayState implements State {
public void doAction() {
System.out.println("TV is in PLAY mode");
}
}
```
状态控制器类中定义状态成员变量,并定义状态切换方法:
```
public class TVContext {
private State state;
public void setState(State state) {
this.state = state;
}
public void doAction() {
state.doAction();
}
}
```
在客户端代码中测试状态切换:
```
public class StateTest {
public static void main(String[] args) {
TVContext context = new TVContext();
context.setState(new TVStartState());
context.doAction();
context.setState(new TVStopState());
context.doAction();
context.setState(new TVPlayState());
context.doAction();
}
}
```
运行结果:
```
TV is turned ON
TV is turned OFF
TV is in PLAY mode
```
2. 枚举类型
Java语言中的枚举类型是一种特殊的类,可以作为状态类的一种实现方式,省去了状态类的定义和多态的实现。
例如,实现一个简单的红绿灯控制器,使用枚举类型作为状态:
```
public enum TrafficLightState {
RED {
public TrafficLightState next() {
return GREEN;
}
},
GREEN {
public TrafficLightState next() {
return YELLOW;
}
},
YELLOW {
public TrafficLightState next() {
return RED;
}
};
public abstract TrafficLightState next();
}
public class TrafficLightController {
private TrafficLightState state;
public TrafficLightController() {
state = TrafficLightState.RED;
}
public void nextState() {
state = state.next();
System.out.println("Traffic light changes to " + state);
}
}
```
在客户端代码中测试状态切换:
```
public class StateTest {
public static void main(String[] args) {
TrafficLightController controller = new TrafficLightController();
for (int i = 0; i < 10; i++) {
controller.nextState();
}
}
}
```
运行结果:
```
Traffic light changes to GREEN
Traffic light changes to YELLOW
Traffic light changes to RED
Traffic light changes to GREEN
Traffic light changes to YELLOW
Traffic light changes to RED
Traffic light changes to GREEN
Traffic light changes to YELLOW
Traffic light changes to RED
Traffic light changes to GREEN
```
3. 设计模式结合
状态模式可以和其他设计模式结合使用,例如,与观察者模式结合使用,实现状态变化时对观察者的通知。
例如,实现一个简单的风扇状态控制器并通知观察者,使用状态模式和观察者模式结合实现:
```
public interface FanState {
void doAction(FanController controller);
}
public class FanController implements Observable {
private FanState state;
private List
public void setState(FanState state) {
this.state = state;
notifyObservers();
}
public void doAction() {
state.doAction(this);
}
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(this);
}
}
}
public class FanOffState implements FanState {
public void doAction(FanController controller) {
System.out.println("Fan is turned off");
controller.setState(new FanLowState());
}
}
public class FanLowState implements FanState {
public void doAction(FanController controller) {
System.out.println("Fan is set to low speed");
controller.setState(new FanMediumState());
}
}
public class FanMediumState implements FanState {
public void doAction(FanController controller) {
System.out.println("Fan is set to medium speed");
controller.setState(new FanHighState());
}
}
public class FanHighState implements FanState {
public void doAction(FanController controller) {
System.out.println("Fan is set to high speed");
controller.setState(new FanOffState());
}
}
public class FanObserver implements Observer {
public void update(Observable observable) {
FanController controller = (FanController) observable;
System.out.println("Fan state changed to " + controller.getState().getClass().getSimpleName());
}
}
public class StateTest {
public static void main(String[] args) {
FanController controller = new FanController();
FanObserver observer = new FanObserver();
controller.registerObserver(observer);
controller.doAction();
controller.doAction();
controller.doAction();
controller.doAction();
}
}
```
运行结果:
```
Fan is turned off
Fan state changed to FanLowState
Fan is set to low speed
Fan state changed to FanMediumState
Fan is set to medium speed
Fan state changed to FanHighState
Fan is set to high speed
Fan state changed to FanOffState
```
扫码咨询 领取资料