状态模式(State Pattern)

2024/3/31 Java进阶

允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它所属的类。

# 1、状态模式案例

有华为、小米、Vivo三个手机,但他们每个都有自己擅长的领域,华为擅长打电话、小米擅长发信息、Vivo擅长展示图片,我们通过内部状态改变,让他们干自己擅长的事。

public interface Phone {
    void call();
    void sendMessage();
    void showImage();
}
1
2
3
4
5
public class HuaweiPhone implements Phone{
    private PhoneState phoneState;

    public HuaweiPhone(PhoneState phoneState) {
        this.phoneState = phoneState;
    }

    @Override
    public void call() {
        System.out.println("华为打电话");
    }

    @Override
    public void sendMessage() {
        phoneState.setPhone(phoneState.getXiaomiPhone());
        phoneState.sendMessage();
    }

    @Override
    public void showImage() {
        phoneState.setPhone(phoneState.getVivoPhone());
        phoneState.showImage();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class XiaomiPhone implements Phone{
    private PhoneState phoneState;

    public XiaomiPhone(PhoneState phoneState) {
        this.phoneState = phoneState;
    }

    @Override
    public void call() {
        phoneState.setPhone(phoneState.getHuaweiPhone());
        phoneState.call();
    }

    @Override
    public void sendMessage() {
        System.out.println("小米发信息");
    }

    @Override
    public void showImage() {
        phoneState.setPhone(phoneState.getVivoPhone());
        phoneState.showImage();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class VivoPhone implements Phone{
    private PhoneState phoneState;

    public VivoPhone(PhoneState phoneState) {
        this.phoneState = phoneState;
    }

    @Override
    public void call() {
        phoneState.setPhone(phoneState.getHuaweiPhone());
        phoneState.call();
    }

    @Override
    public void sendMessage() {
        phoneState.setPhone(phoneState.getXiaomiPhone());
        phoneState.sendMessage();
    }

    @Override
    public void showImage() {
        System.out.println("vivo展示图片");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Data
public class PhoneState implements Phone{
    private Phone huaweiPhone;
    private Phone xiaomiPhone;
    private Phone vivoPhone;
    private Phone phone;

    public PhoneState() {
        this.huaweiPhone = new HuaweiPhone(this);
        this.xiaomiPhone = new XiaomiPhone(this);
        this.vivoPhone = new VivoPhone(this);
        phone = this.huaweiPhone;
    }

    @Override
    public void call() {
        phone.call();
    }

    @Override
    public void sendMessage() {
        phone.sendMessage();
    }

    @Override
    public void showImage() {
        phone.showImage();
    }
}
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
public class ClientTest {
    public static void main(String[] args) {
        Phone phone = new PhoneState();
        phone.call();
        phone.sendMessage();
        phone.showImage();
        System.out.println("===============");
        phone.call();
        phone.sendMessage();
        phone.showImage();
    }
}
/**
 * 输出结果:
 * 华为打电话
 * 小米发信息
 * vivo展示图片
 * ===============
 * 华为打电话
 * 小米发信息
 * vivo展示图片
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22