一、简介
1.1 模式定义
允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。其别名为状态对象,状态模式是一种对象行为型模式。
1.2 适用场景
1)对象的行为依赖于它的状态(属性)并且可以根据它的状态改变而改变它的相关行为,如银行账号,其有不同的状态时其行为有所差异(有些状态即能存款又能取款,有些状态能存款但是不能取款)。
2)代码中包含大量与对象有关的条件语句,这些条件语句的出现没回导致代码的可维护性和灵活性变差,不能方便地增加和删除状态,使客户类与类库之间的耦合增强。在这些条件语句中包含了对象的行为,而且这些条件对应于对象的各种状态。
1.3 优点
1)封装了转换规则。在状态模式中无须使用沉长的条件语句来进行状态的判断和转移,将不同状态之间的转换封装在状态类中,提高了代码的可维护性。
2)枚举可能的状态,在枚举状态之前需要确定状态种类。
3)将所有与某个状态有关的行为放到一个类中,并且可以方便地增加新的状态,只需要改变对象状态即可改变对象的行为。
4)允许状态转换逻辑与状态对象合成一体,而不是 一个巨大的条件语句块。
5)可以让对个环境对象共享一个状态对象,从而减少系统中对象的个数。
1.4 缺点
1)状态模式的使用必然会增加系统类和对象的个数。
2)状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码混乱。
3)状态模式对“开闭原则”的支持并不太好,对于可以切换状态的状态模式,增加新的状态类须要修改那些负责状态转化的源代码,否则无法切换到新增状态;而且修改某个状态类的行为也需修改对应类的源代码。
二、示例:状态模式
2.1 结构图
2.2 环境类ForumAccount(论坛账号类)
public class ForumAccount {
private AbsteactState state;
private String name;
public ForumAccount(String name) {
this.state = new PrimaryState(this);
this.name = name;
System.out.println(name + "注册成功!");
System.out.println("----------------------");
}
public AbsteactState getState() {
return state;
}
public void setState(AbsteactState state) {
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void downloadFile(int scor) {
state.downloadFiel(scor);
}
public void writeNote(int score) {
state.writeNote(score);
}
public void replyNote(int score) {
state.replyNote(score);
}
}
2.3 抽象状态类AbstractState(账号状态类)
public abstract class AbsteactState {
protected ForumAccount acc;
protected int point;
protected String stateName;
public abstract void checkState(int score);
public void downloadFiel(int score) {
System.out.println(acc.getName() + "下载文件,扣除" + score + "积分");
this.point -= score;
checkState(score);
System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
}
public void writeNote(int score) {
System.out.println(acc.getName() + "发布留言,增加" + score + "积分");
this.point += score;
checkState(score);
System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
}
public void replyNote(int score) {
System.out.println(acc.getName() + "回复留言,增加" + score + "积分");
this.point += score;
checkState(score);
System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
}
2.4 具体状态类PrimaryState (新手状态类)
public class PrimaryState extends AbsteactState{
public PrimaryState(AbsteactState state) {
this.acc = state.acc;
this.point = state.point;
this.stateName = "新手";
}
public PrimaryState(ForumAccount acc) {
this.acc = acc;
this.point = 0;
this.stateName = "新手";
}
@Override
public void downloadFiel(int score) {
System.out.println("对不起," + acc.getName() + ",您没有下载文件的权限!");
}
@Override
public void checkState(int score) {
if (point >= 1000) {
acc.setState(new HighState(this));
} else if (point >= 100) {
acc.setState(new MiddleState(this));
}
}
}
2.5 具体状态类MiddleState(高手状态类)
2.6 具体状态类HighState(专家状态类)
public class HighState extends AbsteactState{
public HighState(AbsteactState state) {
this.acc = state.acc;
this.point = state.point;
this.stateName = "专家";
}
public void downloadFiel(int score) {
System.out.println(acc.getName() + "下载文件,扣除" + score + "/2积分");
this.point -= score/2;
checkState(score);
System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
}
public void writeNote(int score) {
System.out.println(acc.getName() + "发布留言,增加" + score + "*2个积分");
this.point += score*2;
checkState(score);
System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
}
@Override
public void checkState(int score) {
if (point < 0) {
System.out.println("余额不足,文件下载失败!");
this.point += score;
} else if (point <= 100) {
acc.setState(new PrimaryState(this));
} else if (point <= 100) {
acc.setState(new MiddleState(this));
}
}
}
2.7 使用示例Deme类
public static void main(String[] args) {
ForumAccount account = new ForumAccount("张三");
account.writeNote(20);
System.out.println("------------------------------------");
account.downloadFile(20);
System.out.println("------------------------------------");
account.replyNote(100);
System.out.println("------------------------------------");
account.writeNote(40);
System.out.println("------------------------------------");
account.downloadFile(80);
System.out.println("------------------------------------");
account.downloadFile(150);
System.out.println("------------------------------------");
account.writeNote(100);
System.out.println("------------------------------------");
account.downloadFile(80);
System.out.println("------------------------------------");
}
}