• 状态模式-对象状态及其转换


     某信用卡业务系统,银行账户存在3种状态,且在不同状态下存在不同的行为:

    1)正常状态(余额大等于0),用户可以存款也可以取款;

    2)透支状态(余额小于0且大于-2000),用户可以存款也可以取款,但需要对欠款支付利息。

    3)受限状态(余额小等于-2000),用户只能存款,还需要对欠款支付利息。

    图 伪代码实现上述需求

    上面代码存在以下问题:

    1)获取状态时,有好多个if分支,如果再增加几个状态,则需要增加判断条件,同时也不符合开闭原则

    2)在进行存取款操作时,有对状态进行判断的条件,行为受到状态的限制。

    为了更好对具有多种状态的对象进行设计,可以使用一种被称作状态模式的设计模式

    1 状态模式

    状态模式(State Pattern)允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的状态类。是一种对象行为型模式。

    图 状态模式UML

    Context:环境类,是拥有多种状态的对象。由于环境类的状态存在多样性且在不同状态下对象的行为有所不同,因此将状态独立出去形成单独的状态类。

    State:抽象状态类,用于定义一个接口以封装与环境类的一个特定状态相关的行为。在抽象状态类中声明各种不同状态对应的方法,而在其子类中实现这些方法。

    ConcreteState:具体状态类,是抽象状态类的子类,每个子类实现与环境类的一个状态相关的行为。

    1. public class UserAccount {
    2. private double balance;
    3. private CardState cardState;
    4. public UserAccount(double balance) {
    5. this.balance = balance;
    6. cardState = new NormalCardState(this);
    7. }
    8. public double getBalance() {
    9. return balance;
    10. }
    11. public void setBalance(double balance) {
    12. this.balance = balance;
    13. }
    14. public void deposit(double money) {
    15. System.out.println("存钱 " + money);
    16. cardState.deposit(money);
    17. changeState();
    18. System.out.println("信用卡余额:"+ balance + ",状态是:" + cardState.getState());
    19. System.out.println("------------------------------");
    20. }
    21. public void withdraw(double money) {
    22. if (balance - money < 0) {
    23. System.out.println("借钱 " + money + ",利息利率是0.01");
    24. } else {
    25. System.out.println("取款 " + money);
    26. }
    27. cardState.withdraw(money);
    28. changeState();
    29. System.out.println("信用卡余额:"+ balance + ",状态是:" + cardState.getState());
    30. System.out.println("------------------------------");
    31. }
    32. public void changeState() {
    33. if (balance > 0) {
    34. if (!"正常".equals(cardState.getState())) cardState = new NormalCardState(this);
    35. } else if (balance > -2000) {
    36. if (!"透支".equals(cardState.getState())) cardState = new OverdraftCardState(this);
    37. } else {
    38. if (!"受限".equals(cardState.getState())) cardState = new LimitationCardState(this);
    39. }
    40. }
    41. public void setCardState(CardState cardState) {
    42. this.cardState = cardState;
    43. }
    44. }
    45. public abstract class CardState {
    46. protected final UserAccount userAccount;
    47. public CardState(UserAccount userAccount) {
    48. this.userAccount = userAccount;
    49. }
    50. public abstract void deposit(double money); // 存款
    51. public abstract void withdraw(double money); // 取款
    52. public abstract void payInterest(); // 支付利息
    53. public abstract String getState(); // 获取状态
    54. }
    55. public class BankService {
    56. public static void main(String[] args) {
    57. // 开户
    58. UserAccount userAccount = new UserAccount(1000);
    59. userAccount.withdraw(500);
    60. userAccount.deposit(200);
    61. userAccount.withdraw(1000);
    62. userAccount.deposit(100);
    63. userAccount.withdraw(2000);
    64. userAccount.withdraw(500);
    65. }
    66. }
    67. //取款 500.0
    68. //信用卡余额:500.0,状态是:正常
    69. //------------------------------
    70. //存钱 200.0
    71. //信用卡余额:700.0,状态是:正常
    72. //------------------------------
    73. //借钱 1000.0,利息利率是0.01
    74. //信用卡余额:-300.0,状态是:透支
    75. //------------------------------
    76. //存钱 100.0
    77. //支付利息:-3.0
    78. //信用卡余额:-203.0,状态是:透支
    79. //------------------------------
    80. //借钱 2000.0,利息利率是0.01
    81. //支付利息:-2.0300000000000002
    82. //信用卡余额:-2205.03,状态是:受限
    83. //------------------------------
    84. //借钱 500.0,利息利率是0.01
    85. //该账户已受限,不能取款
    86. //信用卡余额:-2205.03,状态是:受限
    87. //------------------------------
    88. public class NormalCardState extends CardState{
    89. public NormalCardState(UserAccount userAccount) {
    90. super(userAccount);
    91. }
    92. @Override
    93. public void deposit(double money) {
    94. userAccount.setBalance(userAccount.getBalance() + money);
    95. }
    96. @Override
    97. public void withdraw(double money) {
    98. userAccount.setBalance(userAccount.getBalance() - money);
    99. }
    100. @Override
    101. public void payInterest() {
    102. }
    103. @Override
    104. public String getState() {
    105. return "正常";
    106. }
    107. }
    108. public class OverdraftCardState extends CardState{
    109. public OverdraftCardState(UserAccount userAccount) {
    110. super(userAccount);
    111. }
    112. @Override
    113. public void deposit(double money) {
    114. payInterest();
    115. userAccount.setBalance(userAccount.getBalance() + money);
    116. }
    117. @Override
    118. public void withdraw(double money) {
    119. payInterest();
    120. userAccount.setBalance(userAccount.getBalance() - money);
    121. }
    122. @Override
    123. public void payInterest() {
    124. System.out.println("支付利息:" + userAccount.getBalance() * 0.01);
    125. userAccount.setBalance(userAccount.getBalance() * ( 1 + 0.01));
    126. }
    127. @Override
    128. public String getState() {
    129. return "透支";
    130. }
    131. }
    132. public class LimitationCardState extends CardState{
    133. public LimitationCardState(UserAccount userAccount) {
    134. super(userAccount);
    135. }
    136. @Override
    137. public void deposit(double money) {
    138. payInterest();
    139. userAccount.setBalance(userAccount.getBalance() + money);
    140. }
    141. @Override
    142. public void withdraw(double money) {
    143. System.out.println("该账户已受限,不能取款");
    144. }
    145. @Override
    146. public void payInterest() {
    147. System.out.println("支付利息:" + userAccount.getBalance() * 0.01);
    148. userAccount.setBalance(userAccount.getBalance() * ( 1 + 0.01));
    149. }
    150. @Override
    151. public String getState() {
    152. return "受限";
    153. }
    154. }

    使用状态模式后,在编码过程中,可以不要在关系具体状态,只需专注实现具体状态下的业务。

    1.1 状态转换方式

    在状态模式中,环境类的状态转换方式有两种:

    1)在环境类完成转换。(上面代码是以这种形式)

    2)在具体状态类中完成转换。

    图 两种状态转换方式的比较

    如果新增状态类,则两种方式都需要在各自的类中做修改。都不符合开闭原则。

    1.2 共享状态

    在有些情况下,多个环境类对象需要共享一个状态,这时需要把状态对象定义为一个静态成员对象。

    需求:一个房间有两个开关来控制灯泡的开关。开关等功能是固定的(打开只能使灯泡亮起,关闭只能使灯泡熄灭。

    1. public class LightSwitch {
    2. private final static LightState onState = new OnLightState(),offState = new OffLightState();
    3. private static LightState lightState = offState;
    4. private final String name;
    5. public LightSwitch(String name) {
    6. this.name = name;
    7. }
    8. public static void changeLightState(String type) {
    9. if ("on".equalsIgnoreCase(type)) {
    10. lightState = onState;
    11. } else {
    12. lightState = offState;
    13. }
    14. }
    15. public void off() {
    16. System.out.println(name + "关闭操作");
    17. lightState.off(this);
    18. }
    19. public void on() {
    20. System.out.println(name + "打开操作");
    21. lightState.on(this);
    22. }
    23. }
    24. public abstract class LightState {
    25. public abstract void on(LightSwitch lightSwitch);
    26. public abstract void off(LightSwitch lightSwitch);
    27. }
    28. public class OnLightState extends LightState{
    29. @Override
    30. public void on(LightSwitch lightSwitch) {
    31. System.out.println("灯泡已打开");
    32. System.out.println("--------------");
    33. }
    34. @Override
    35. public void off(LightSwitch lightSwitch) {
    36. System.out.println("关闭成功");
    37. LightSwitch.changeLightState("off");
    38. System.out.println("--------------");
    39. }
    40. }
    41. public class OffLightState extends LightState{
    42. @Override
    43. public void on(LightSwitch lightSwitch) {
    44. System.out.println("打开成功");
    45. LightSwitch.changeLightState("on");
    46. System.out.println("--------------");
    47. }
    48. @Override
    49. public void off(LightSwitch lightSwitch) {
    50. System.out.println("灯泡已关闭");
    51. System.out.println("--------------");
    52. }
    53. }
    54. public class PeopleOpera {
    55. public static void main(String[] args) {
    56. LightSwitch lightSwitch1 = new LightSwitch("开关1");
    57. LightSwitch lightSwitch2 = new LightSwitch("开关2");
    58. lightSwitch1.on();
    59. lightSwitch2.off();
    60. lightSwitch1.off();
    61. lightSwitch1.on();
    62. lightSwitch2.on();
    63. lightSwitch2.off();
    64. }
    65. }
    66. //开关1打开操作
    67. //打开成功
    68. //--------------
    69. //开关2关闭操作
    70. //关闭成功
    71. //--------------
    72. //开关1关闭操作
    73. //灯泡已关闭
    74. //--------------
    75. //开关1打开操作
    76. //打开成功
    77. //--------------
    78. //开关2打开操作
    79. //灯泡已打开
    80. //--------------
    81. //开关2关闭操作
    82. //关闭成功
    83. //--------------

    2 优缺点

    优点:

    1)环境类转换状态方式,封装状态的转换规则,对状态转换代码集中管理。

    2)将所有与具体状态有关的行为都封装在一个类中。

    3)可以让多个环境对象共享一个状态对象,从而减少系统中对象个数。

    4)在具体状态类中转换状态方式,将状态转换逻辑与状态对象合成一起,避免使用庞大的条件语句块来将业务方法和状态转换代码交织在一起。

    缺点:

    1)增加了类和对象的个数。

    2)实现较为复杂,增加了系统设计难度。

    3)对开闭原则的支持并不好。

    3 适用场景

    1)对象的行为依赖它的状态,且状态之间互相转换。

    2)代码中包含大量与对象状态有关的条件语句。

  • 相关阅读:
    竞赛 深度学习+opencv+python实现车道线检测 - 自动驾驶
    java MINio 操作工具类
    SpringBoot接收参数的三种方式:
    vivado查看报告和消息5
    WebDAV之π-Disk派盘 + 言叶
    maven 3.8.3 安装并配置的步骤
    iOS APP启动广告实现方式 与 APP唤端调用
    2023山东养老展|第五届中国(山东)国际养老服务业展览会
    C语言——二维数组作为函数的参数,求二维数组的最大值
    尚医通 (十五) --------- 平台管理前端搭建
  • 原文地址:https://blog.csdn.net/qq_25308331/article/details/134067231