<>状态模式

<>角色

* 上下文(State)
定义了客户端需要的接口,内部维护一个当前状态,并负责具体状态的切换。

* 抽象状态(State)
定义一个接口,用以封装环境对象中的特定状态所对应的行为,可以有一个或多个行为。

* 具体状态(Concrete State)
实现抽象状态所对应的行为,并且在需要的情况下进行状态切换。

类图:

<>案例

<>银行账号状态

Account: 环境类
public class Account { private AccountState state; //维持一个对抽象状态对象的引用 private
String owner; //开户名 private double balance = 0; //账户余额 public Account(String
owner,double init) { this.owner = owner; this.balance = balance; this.state =
new NormalState(this); //设置初始状态 System.out.println(this.owner + "开户,初始金额为" +
init); System.out.println("---------------------------------------------"); }
public double getBalance() { return this.balance; } public void setBalance(
double balance) { this.balance = balance; } public void setState(AccountState
state) { this.state = state; } public void deposit(double amount) { System.out.
println(this.owner + "存款" + amount); state.deposit(amount); //调用状态对象的deposit()方法
System.out.println("现在余额为"+ this.balance); System.out.println("现在帐户状态为"+ this.
state.getClass().getName()); System.out.println(
"---------------------------------------------"); } public void withdraw(double
amount) { System.out.println(this.owner + "取款" + amount); state.withdraw(amount)
; //调用状态对象的withdraw()方法 System.out.println("现在余额为"+ this.balance); System.out.
println("现在帐户状态为"+ this. state.getClass().getName()); System.out.println(
"---------------------------------------------"); } public void computeInterest(
){ state.computeInterest(); //调用状态对象的computeInterest()方法 } }
AcountState: 抽象状态
public abstract class AccountState { protected Account acc; public abstract
void deposit(double amount); //取款 public abstract void withdraw(double amount);
//透支 public abstract void computeInterest(); public abstract void stateCheck();
}
NormalState: 正常状态
public class NormalState extends AccountState { public NormalState(Account acc)
{ this.acc = acc; } public NormalState(AccountState state) { this.acc = state.
acc; } @Override public void deposit(double amount) { acc.setBalance(acc.
getBalance() + amount); stateCheck(); } @Override public void withdraw(double
amount) { acc.setBalance(acc.getBalance() - amount); stateCheck(); } @Override
public void computeInterest() { System.out.println("正常状态,无须支付利息!"); } @Override
public void stateCheck() { if (acc.getBalance() > -2000 && acc.getBalance() <= 0
) { acc.setState(new OverdraftState(this)); } else if (acc.getBalance() == -2000
) { acc.setState(new RestrictedState(this)); } else if (acc.getBalance() < -2000
) { System.out.println("操作受限!"); } } }
OverdraftState: 透支状态
public class OverdraftState extends AccountState { public OverdraftState(
AccountState state) { this.acc = state.acc; } public void deposit(double amount)
{ acc.setBalance(acc.getBalance() + amount); stateCheck(); } public void
withdraw(double amount) { acc.setBalance(acc.getBalance() - amount); stateCheck(
); } public void computeInterest() { System.out.println("计算利息!"); } //状态转换
public void stateCheck() { if (acc.getBalance() > 0) { acc.setState(new
NormalState(this)); } else if (acc.getBalance() == -2000) { acc.setState(new
RestrictedState(this)); } else if (acc.getBalance() < -2000) { System.out.
println("操作受限!"); } } }
RestrictedState: 受限状态
public class RestrictedState extends AccountState { public RestrictedState(
AccountState state) { this.acc = state.acc; } public void deposit(double amount)
{ acc.setBalance(acc.getBalance() + amount); stateCheck(); } public void
withdraw(double amount) { System.out.println("帐号受限,取款失败"); } public void
computeInterest() { System.out.println("计算利息!"); } //状态转换 public void stateCheck
() { if(acc.getBalance() > 0) { acc.setState(new NormalState(this)); } else if(
acc.getBalance() > -2000) { acc.setState(new OverdraftState(this)); } } }
Main:
public class Main { public static void main(String[] args) { Account acc = new
Account("evanp",0.0); acc.deposit(1000); acc.withdraw(2000); acc.deposit(3000);
acc.withdraw(4000); acc.withdraw(1000); acc.computeInterest(); } }
Output:
evanp开户,初始金额为0.0 --------------------------------------------- 段誉存款1000.0
现在余额为1000.0 现在帐户状态为NormalState ---------------------------------------------
段誉取款2000.0 现在余额为-1000.0 现在帐户状态为OverdraftState
--------------------------------------------- 段誉存款3000.0 现在余额为2000.0
现在帐户状态为NormalState --------------------------------------------- 段誉取款4000.0
现在余额为-2000.0 现在帐户状态为RestrictedState
--------------------------------------------- 段誉取款1000.0 帐号受限,取款失败 现在余额为-2000.0
现在帐户状态为RestrictedState --------------------------------------------- 计算利息!
<>状态模式的优缺点

优点:

* 封装了状态的转换规则,在状态模式中可以将状态的转换代码封装在环境类或者具体状态类中,可以对状态转换代码进行集中管理,而不是分散在一个个业务方法中。
* 将所有与某个状态有关的行为放到一个类中,只需要注入一个不同的状态对象即可使环境对象拥有不同的行为。
* 允许状态转换逻辑与状态对象合成一体,而不是提供一个巨大的条件语句块,状态模式可以让我们避免使用庞大的条件语句来将业务方法和状态转换代码交织在一起。
* 可以让多个环境对象共享一个状态对象,从而减少系统中对象的个数。
缺点:

* 会增加系统中类和对象的个数,导致系统运行开销增大。
* 结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱,增加系统设计的难度。
* 对“开闭原则”的支持并不太好,增加新的状态类需要修改那些负责状态转换的源代码,否则无法转换到新增状态;而且修改某个状态类的行为也需修改对应类的源代码。
<>应用场景

* 对象的行为依赖于它的状态(如某些属性值),状态的改变将导致行为的变化。
*
在代码中包含大量与对象状态有关的条件语句,这些条件语句的出现,会导致代码的可维护性和灵活性变差,不能方便地增加和删除状态,并且导致客户类与类库之间的耦合增强。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信