状态模式

在状态模式中, 类的行为时给予它的状态改变的。折中类型的设计模式属于行为型模式。

定义

当一个对象内在状态改变时允许其改变行为,这个对象看起来像改变了其类。

状态模式通用类图

实现

抽象状态角色State

接口或者抽象类,负责对象状态的定义,并且封装环境角色以实现状态切换。

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface IState {
void open();
void close();
void run();
void stop();
}

public abstract class BaseState implements IState {
protected Car context;
public void setContext(Car context) {
this.context = context;
}
}

具体状态角色ConcreteState

每个具体状态必须完成两个职责:本状态下要做的事情以及本状态如何过渡到其他状态。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class OpenState extends BaseState {
public void open() {
super.context.setCurrentState(Car.OPEN_STATE);
System.out.println("车门已开启");
}
public void close() {
super.context.setCurrentState(Car.CLOSE_STATE);
System.out.println("关闭车门。。。");
}
public void run() {
System.out.println("开门状态下不允许飙车start");
}
public void stop() {
System.out.println("开门状态下不允许飙车Stop");
}
}


public class StopState extends BaseState {
public void open() {
super.context.setCurrentState(Car.OPEN_STATE);
System.out.println("停车后开启车门");
}
public void close() {
super.context.setCurrentState(Car.CLOSE_STATE);
System.out.println("停车后关闭车门");
}
public void run() {
super.context.setCurrentState(Car.RUN_STATE);
System.out.println("停车后再次跑起来");
}
public void stop() {
super.context.setCurrentState(Car.STOP_STATE);
System.out.println("停车后。。停止不同");
}
}

public class RunState extends BaseState {
public void open() {
System.out.println("飙车时不能开门");
}
public void close() {
System.out.println("飙车时车门已关闭");
}
public void run() {
super.context.setCurrentState(Car.RUN_STATE);
System.out.println("飙车Run");
}
public void stop() {
super.context.setCurrentState(Car.STOP_STATE);
System.out.println("飙车 stop");
}
}

public class CloseState extends BaseState {
public void open() {
super.context.setCurrentState(Car.OPEN_STATE);
System.out.println("开启车门。。。");
}
public void close() {
System.out.println("车门已经关闭!");
}
public void run() {
this.context.setCurrentState(Car.RUN_STATE);
System.out.println("汽车开始运行。。。");
}
public void stop() {
this.context.setCurrentState(Car.STOP_STATE);
System.out.println("汽车停止。");
}
}

环境角色Context

定义客户端需要的接口,并且负责具体状态的切换。

把状态对象声明为静态常量,有几个状态对象就声明几个静态常量。
环境角色具有状态抽象角色定义的所有行为,具体执行使用委托方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Car implements IState {

public static final BaseState OPEN_STATE = new OpenState();
public static final BaseState CLOSE_STATE = new CloseState();
public static final BaseState RUN_STATE = new RunState();
public static final BaseState STOP_STATE = new StopState();
private BaseState currentState;
public Car() {
this.setCurrentState(CLOSE_STATE);
}
public BaseState getCurrentState() {
return currentState;
}
public Car setCurrentState(BaseState currentState) {
this.currentState = currentState;
this.currentState.setContext(this);
return this;
}
public void open() {
this.currentState.open();
}
public void close() {
this.currentState.close();
}
public void run() {
this.currentState.run();
}
public void stop() {
this.currentState.stop();
}
}

使用

结合建造者模式将已有的状态按照一定的顺序再重新组装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Car car = new Car();

car.setCurrentState(Car.CLOSE_STATE);

car.run(); //汽车开始运行。。。
car.stop(); //飙车 stop

car.run(); //停车后再次跑起来
car.open(); //飙车时不能开门

car.stop(); //飙车 stop

car.open(); //停车后开启车门
car.close(); //关闭车门。。。

应用

优点

  • 结构清晰
    避免过多的switch case和if else,降低了程序的复杂性,提高系统的可维护性。
  • 遵循设计原则
    遵循了开闭原则以及单一职责原则,每个状态都是一个子类,要增加状态就要增加子类,修改状态时只修改一个子类即可。
  • 封装性良好
    状态变换防止到类的内部来实现,外部调用不需要知道类内部如何实现状态和行为的变换。

缺点

  • 状态过多时子类膨胀

使用场景

  • 行为跟随状态改变而改变的场景
  • 条件、分支判断语句的替代

策略模式

策略模式使用的就是面向对象的继承和多态机制。

定义

定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。

策略模式通用类图

实现

Strategy 抽象策略角色

策略算法的抽象,通常是接口,定义每个策略或者算法必须具有的方法和属性。

1
2
3
public interface Strategy{
void doSomething();
}

ConcreteStrategy 具体策略角色

实现抽象策略的具体算法。

1
2
3
4
5
6
7
8
9
10
11
public class ConcreteStrategy1 implements Strategy{
public void doSomething(){
//Do Strategy1.
}
}

public class ConcreteStrategy2 implements Strategy{
public void doSomething(){
//Do Strategy2.
}
}

Context 封装角色

封装上下文,屏蔽高层模块对策略、算法的直接访问,封装可能存在的变化。
策略模式的重点就是封装角色,它借用了代理模式的思路。

1
2
3
4
5
6
7
8
9
public class Context{
private Strategy strategy = null;
public Context(Strategy strategy){
this.strategy = strategy;
}
public void doAny(){
this.strategy.doSomething();
}
}

Use

1
2
3
Strategy strategy = new ConcreteStrategy1();
Context context = new Context(strategy);
context.doAny(strategy);

应用

优点

  • 算法可以自由切换
    只需要实现策略接口类,通过封装角色对其进行封装即可。
  • 避免使用多重条件判断
    1
    2
    3
    4
    5
    6
    if (condition1){
    strategy1.do();
    }else if(condition2){
    strategy2.do();
    }
    // .....
  • 扩展性好

缺点

  • 策略类数量多
  • 所有策略类都需要对外暴露
    上层模块必须知道有哪些策略, 然后才能决定使用哪一个策略,与迪米特法则相违背。
    (迪米特法则:一个类对于其他类知道的越少越好)

注意事项

当具体的策略数量超过一定个数时,考虑使用混合模式,解决策略类膨胀和对外暴露的问题,否则会带来很大的系统维护成本。

使用场景

  • 多个类只有在算法或者行为上稍有不同的场景
  • 算法需要自由切换的场景
  • 需要屏蔽算法规则的场景

策略枚举

当策略枚举不经常发生变化时可以使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public enum StrategyEnum {

ADD("+") {
@Override
public int exec(int a, int b) {
return a + b;
}
},
SUB("-") {
@Override
public int exec(int a, int b) {
return a - b;
}
};

private String opt;
StrategyEnum(String opt) {
this.opt = opt;
}

abstract int exec(int a, int b);
}