状态模式与策略模式很像,真的很像。

下面举个例子来说明,我们都知道银行经常将用户划分个三六九等,划分的方法很简单,就是用户的存款。下面用php代码来模拟下这个过程:

  1. <?php
  2. /**
  3. *  状态模式的例子
  4. *
  5. *  Copyright(c) 2012 by ustb80. All rights reserved
  6. *
  7. *  To contact the author write to {@link mailto:ustb80@163.com}
  8. *
  9. * @author ustb80
  10. * @version $Id: State.php,v 1.0 2012-11-01
  11. * @package
  12. */
  13. // ------------------------------------------------------------------------
  14. $config = array('new'=>10, 'silver'=>10000, 'gold'=>999999999999);
  15. /**
  16. * 状态抽象类
  17. *
  18. * @author ustb80
  19. */
  20. abstract class State
  21. {
  22. protected $client;
  23. protected $silver_limit = 10;
  24. protected $gold_limit = 10000;
  25. abstract function deposit($amount);// 存款
  26. abstract function withdraw($amount);// 取款
  27. protected function changeState()
  28. {
  29. global $config;
  30. if (!empty($config))
  31. {
  32. foreach ($config as $key => $val)
  33. {
  34. if ($this->client->balance < $val)
  35. {
  36. $state_name = ucfirst($key)."State";
  37. echo "亲,你已经成为{$key}卡用户\n";
  38. $this->client->setState($this->client->$state_name);
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. // 实现各种乱七八糟的状态
  46. // 新用户
  47. class NewState extends State
  48. {
  49. public function __construct($client)
  50. {
  51. $this->client = $client;
  52. }
  53. public function deposit($amount)
  54. {
  55. $this->client->balance += $amount;
  56. $this->changeState();
  57. }
  58. public function withdraw($amount)
  59. {
  60. die("亲,你还没存钱呢就想着取钱呀,门儿都没有!\n");
  61. }
  62. }
  63. // 银卡用户
  64. class SilverState extends State
  65. {
  66. public function __construct($client)
  67. {
  68. $this->client = $client;
  69. }
  70. public function deposit($amount)
  71. {
  72. $this->client->balance += $amount;
  73. $this->changeState();
  74. }
  75. public function withdraw($amount)
  76. {
  77. if ($this->balance < $amount)
  78. {
  79. die("亲,余额不足\n");
  80. }
  81. $this->client->balance -= $amount;
  82. $this->changeState();
  83. }
  84. }
  85. // 金卡用户
  86. class GoldState extends State
  87. {
  88. public function __construct($client)
  89. {
  90. $this->client = $client;
  91. }
  92. public function deposit($amount)
  93. {
  94. $this->balance += $amount;
  95. }
  96. public function withdraw($amount)
  97. {
  98. if ($this->client->balance < $amount)
  99. {
  100. die("亲,余额不足\n");
  101. }
  102. $this->client->balance -= $amount;
  103. $this->changeState();
  104. }
  105. }
  106. // -------------------------------------------------------------
  107. /**
  108. * 银行客户类
  109. *
  110. * @author ustb80
  111. */
  112. class BankClient
  113. {
  114. public $balance;
  115. private $state;
  116. public function __construct()
  117. {
  118. global $config;
  119. foreach ($config as $key => $val)
  120. {
  121. $method_name = ucfirst($key)."State";
  122. $this->$method_name = new $method_name($this);
  123. }
  124. // 初始化状态
  125. $this->state = $this->NewState;
  126. }
  127. /**
  128. * 存钱
  129. *
  130. * @param float $amount 存入金额
  131. */
  132. public function deposit($amount)
  133. {
  134. $this->state->deposit($amount);
  135. }
  136. /**
  137. * 取钱
  138. *
  139. * @param float $amount 取出金额
  140. */
  141. public function withdraw($amount)
  142. {
  143. $this->state->withdraw($amount);
  144. }
  145. /**
  146. * 变更状态
  147. *
  148. * @param object $state
  149. */
  150. public function setState($state)
  151. {
  152. $this->state = $state;
  153. }
  154. /**
  155. * 查看余额
  156. */
  157. public function getBalance()
  158. {
  159. return $this->balance;
  160. }
  161. }
  162. // 测试代码
  163. $BankClient = new BankClient();
  164. $BankClient->deposit(10000);
  165. $BankClient->withdraw(5000);
  166. $BankClient->deposit(100000);
  167. // 查看余额
  168. echo $BankClient->getBalance();

上面用了一个配置数组来动态创建对象实例。

输出结果:

  1. 亲,你已经成为gold卡用户
  2. 亲,你已经成为silver卡用户
  3. 亲,你已经成为gold卡用户
  4. 105000
本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1047086,如需转载请自行联系原作者

设计模式学习笔记(6) - 状态模式相关推荐

  1. 设计模式学习笔记——备忘录(Memento)模式

    设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...

  2. 设计模式学习笔记——观察者(Observer)模式

    设计模式学习笔记--观察者(Observer)模式 @(设计模式)[设计模式, 观察者模式, Observer] 设计模式学习笔记观察者Observer模式 基本介绍 观察者案例 类图 实现代码 Ob ...

  3. 设计模式学习笔记——访问者(Visitor)模式

    设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...

  4. 设计模式学习笔记——解释器(Interpreter)模式

    设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...

  5. 设计模式学习笔记——命令(Command)模式

    设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...

  6. 设计模式学习笔记——代理(Proxy)模式

    设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...

  7. 设计模式学习笔记——外观(Facade)模式

    设计模式学习笔记--外观(Facade)模式 @(设计模式)[设计模式, 外观模式, facade] 设计模式学习笔记外观Facade模式 基本介绍 外观案例 类图 实现代码 Database类 ma ...

  8. 设计模式学习笔记——装饰(Decorator)模式

    设计模式学习笔记--装饰(Decorator)模式 @(设计模式)[设计模式, 装饰模式, decorator] 设计模式学习笔记装饰Decorator模式 基本介绍 装饰案例 类图 实现代码 Dis ...

  9. 设计模式学习笔记——组合(Composite)模式

    设计模式学习笔记--组合(Composite)模式 @(设计模式)[设计模式, 组合模式, composite] 设计模式学习笔记组合Composite模式 基本介绍 组合案例 类图 实现代码 Ent ...

最新文章

  1. 奋斗---论门当户对
  2. 伪共享(false sharing),并发编程无声的性能杀手
  3. oracle修改某个表的字段顺序
  4. java并发:interrupt进程终止
  5. 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换
  6. nginx系列11:负载均衡哈希算法ip_hash与hash模块
  7. mysql中DateTime、Date、Time、TimeStamp区别
  8. 统计学怎么求加权指数_统计学——指数
  9. 伍德里奇计量经济学第三章课后计算机作业,伍德里奇---计量经济学第7章部分计算机习题详解(STATA)...
  10. vs2008 sp1补丁安装到最后一点点的时候,就无法安装下去了 解决方法[转]
  11. IDEA+EmmyLua Lua开发环境搭建
  12. 图片轮播插件slideBox
  13. 【css练习】方格背景
  14. Uri.parse()的各种用法
  15. Vuex入门及进阶笔记
  16. 招聘信息薪资范围是12-20K,能否要20K的薪资?
  17. 计算机在化学中的应用总结感悟,计算机在化学中的应用实践总结报告
  18. 13 | 关于写简历,必须要注意的一些细节
  19. Blender着色器节点教程 —— AO
  20. Android程序员需掌握的JVM知识

热门文章

  1. SpringMVC json/xml自动转换
  2. 以太坊是什么鬼?!媲美比特币的加密币大揭秘
  3. 9.6-9.7 awk
  4. 阿里百川IMSDK--自定义群聊界面
  5. java thread与runnable联系区别
  6. python中时间的加n和减n运算
  7. 使用json 和jQuery制作级联dropdownlist
  8. LuaTinker向Linux移植成功
  9. wlnmp+nginx+mysql+php集合包_LNMP(Linux+Nginx+MySQL+PHP)部署详解(一)
  10. Android 自定义UI--电池