• 设计模式-状态模式-笔记


    状态模式State

    在组件构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的稳定?“状态变化”模式为这一问题提供了一种解决方案。

    经典模式:State、Memento

    动机(Motivation)

    在软件构建过程中,某些对象的状态如何改变,其行为也随之而发生变化,比如文档处于只读状态,其支持的行为和读写状态支持的行为就可以完全不同。

    如何在运行时根据对象的状态来透明地更改对象的行为?而不会为对象操作和状态转化之间引入耦合?

    示例1

    1. enum NetworkState
    2. {
    3. Network_Open,
    4. Network_Close,
    5. Network_Connect,
    6. NetWork_Wait // 新增状态
    7. };
    8. class NetworkProcessor {
    9. NetworkState state_;
    10. public:
    11. void Operation1() {
    12. if (Network_Open == state_) {
    13. //***
    14. state_ = Network_Close;
    15. }
    16. else if(Network_Close == state_)
    17. {
    18. //...
    19. state_ = Network_Connect;
    20. }
    21. else if (Network_Connect == state_) {
    22. //$$$
    23. state_ = Network_Open;
    24. }
    25. // 变化
    26. else if (NetWork_Wait == state_) {
    27. //---
    28. }
    29. }
    30. void Operation2() {
    31. if (Network_Open == state_) {
    32. //***
    33. state_ = Network_Connect;
    34. }
    35. else if (Network_Close == state_)
    36. {
    37. //...
    38. state_ = Network_Open;
    39. }
    40. else if (Network_Connect == state_) {
    41. //$$$
    42. state_ = Network_Close;
    43. }
    44. // 变化
    45. else if (NetWork_Wait == state_) {
    46. //---
    47. }
    48. }
    49. void Operation3() {
    50. //...
    51. }
    52. };

    示例2

    1. class NetworkState {
    2. public:
    3. NetworkState* pNext;
    4. virtual void Operation1() = 0;
    5. virtual void Operation2() = 0;
    6. virtual void Operation3() = 0;
    7. virtual ~NetworkState() {}
    8. };
    9. class OpenState : public NetworkState {
    10. static NetworkState* m_instance;
    11. public:
    12. static NetworkState* getInstance() {
    13. if (nullptr == m_instance) {
    14. m_instance = new OpenState();
    15. }
    16. return m_instance;
    17. }
    18. virtual void Operation1() {
    19. //***
    20. pNext = CloseState::getInstance();
    21. }
    22. virtual void Operation2() {
    23. //...
    24. pNext = ConnectState::getInstance();
    25. }
    26. virtual void Operation3() {
    27. //$$$
    28. pNext = OpenState::getInstance();
    29. }
    30. };
    31. class CloseState : public NetworkState {
    32. static NetworkState* m_instance;
    33. public:
    34. static NetworkState* getInstance() {
    35. if (nullptr == m_instance) {
    36. m_instance = new CloseState();
    37. }
    38. return m_instance;
    39. }
    40. virtual void Operation1() {
    41. //***
    42. pNext = ConnectState::getInstance();
    43. }
    44. virtual void Operation2() {
    45. //...
    46. pNext = OpenState::getInstance();
    47. }
    48. virtual void Operation3() {
    49. //$$$
    50. pNext = CloseState::getInstance();
    51. }
    52. };
    53. class ConnectState : public NetworkState {
    54. static NetworkState* m_instance;
    55. public:
    56. static NetworkState* getInstance() {
    57. if (nullptr == m_instance) {
    58. m_instance = new ConnectState();
    59. }
    60. return m_instance;
    61. }
    62. virtual void Operation1() {
    63. //***
    64. pNext = OpenState::getInstance();
    65. }
    66. virtual void Operation2() {
    67. //...
    68. pNext = CloseState::getInstance();
    69. }
    70. virtual void Operation3() {
    71. //$$$
    72. pNext = ConnectState::getInstance();
    73. }
    74. };
    75. // 扩展
    76. class WaitState : public NetworkState {
    77. //...
    78. };
    79. class NetworkProcessor {
    80. NetworkState* state_;
    81. public:
    82. NetworkProcessor(NetworkState* state) {
    83. this->state_ = state;
    84. }
    85. void Operation1() {
    86. //...
    87. state_->Operation1();
    88. state_ = state_->pNext;
    89. //...
    90. }
    91. void Operation2() {
    92. //...
    93. state_->Operation2();
    94. state_ = state_->pNext;
    95. //...
    96. }
    97. void Operation3() {
    98. //...
    99. state_->Operation3();
    100. state_ = state_->pNext;
    101. //...
    102. }
    103. };

    要点总结

    State模式将所有与一个特定状态的相关的行为都放入一个State的子类对象中,在对象状态切换时,切换相应的对象;但同时维持State的接口,这样实现了具体操作与状态转换之间的解耦。

    为不同的状态引入不同的对象使得状态转换变得更加明确,而且可怀疑保证不会出现状态不一致的情况,因为转换是原子性的---即要么彻底转换过来,要么不转换。

    如果State对象没有实例变量,那么各个上下文可怀疑共享同一个State对象,从而节省对象开销。

  • 相关阅读:
    Unity实现跨场景的传送门
    Git进阶命令-reset
    Maven详细总结
    基于Java NIO 写的一个简单版 Netty 服务端
    A-Level经济真题每期一练(22)
    Centos配置邮件发送
    【通信】基于PUMA实现 DOA 估计附matlab代码
    随笔
    c++模板
    Linux 文件和目录列表(ls 命令)
  • 原文地址:https://blog.csdn.net/zhaodongdong2012/article/details/134471446