image.png状态模式(State Pattern):允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修
改了它的类。其别名为状态对象(Objects for States),状态模式是一种对象行为型模式。
在状态模式结构图中包含如下几个角色:
● Context(环境类):环境类又称为上下文类,它是拥有多种状态的对象。由于环境类的状
态存在多样性且在不同状态下对象的行为有所不同,因此将状态独立出去形成单独的状态
类。在环境类中维护一个抽象状态类State的实例,这个实例定义当前状态,在具体实现时,
它是一个State子类的对象。
● State(抽象状态类):它用于定义一个接口以封装与环境类的一个特定状态相关的行为,在
抽象状态类中声明了各种不同状态对应的方法,而在其子类中实现类这些方法,由于不同状
态下对象的行为可能不同,因此在不同子类中方法的实现可能存在不同,相同的方法可以写
在抽象状态类中。
● ConcreteState(具体状态类):它是抽象状态类的子类,每一个子类实现一个与环境类的一
个状态相关的行为,每一个具体状态类对应环境的一个具体状态,不同的具体状态类其行为
有所不同。
//银行账户:环境类
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()方法
}
}
//抽象状态类
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();
}
-----------------------------------------------------------------------
//正常状态:具体状态类
class NormalState extends AccountState {
public NormalState(Account acc) {
this.acc = acc;
}
public NormalState(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() > -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("操作受限!");
}
}
}
-----------------------------------------------------------------------
//透支状态:具体状态类
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("操作受限!");
}
}
}
-----------------------------------------------------------------------
//受限状态:具体状态类
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));
}
}
}
class Client {
public static void main(String args[]) {
Account acc = new Account("段誉", 0.0);
acc.deposit(1000);
acc.withdraw(2000);
acc.deposit(3000);
acc.withdraw(4000);
acc.withdraw(1000);
acc.computeInterest();
}
}
image.png
共享状态
image.pngclass Switch {
private static State state, onState, offState;
private String name;
public Switch(String name) {
this.name = name;
onState = new OnState();
offState = new OffState();
state = onState;
}
public void setState(State state) {
this.state = state;
}
public void on() {
System.out.print(name);
state.on(this);
}
public void off() {
System.out.print(name);
state.off(this);
}
public static State getState(String type) {
if (type.equalsIgnoreCase("on")) {
return onState;
} else {
return offState;
}
}
}
---------------------------------------------------------------------
abstract class State {
public abstract void on(Switch s);
public abstract void off(Switch s);
}
---------------------------------------------------------------------
class OnState extends State {
public void on(Switch s) {
System.out.println("已经打开!");
}
public void off(Switch s) {
System.out.println("关闭!");
s.setState(Switch.getState("off"));
}
}
---------------------------------------------------------------------
class OffState extends State {
public void on(Switch s) {
System.out.println("打开!");
s.setState(Switch.getState("on"));
}
public void off(Switch s) {
System.out.println("已经关闭!");
}
}
---------------------------------------------------------------------
class Client {
public static void main(String args[]) {
Switch s1, s2;
s1 = new Switch("开关1");
s2 = new Switch("开关2");
s1.on();
s2.on();
s1.off();
s2.off();
s2.on();
s1.on();
}
}
网友评论