设计模式学习笔记(6) - 状态模式

简介:

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

下面举个例子来说明,我们都知道银行经常将用户划分个三六九等,划分的方法很简单,就是用户的存款。下面用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. // ------------------------------------------------------------------------ 
  15.  
  16. $config = array('new'=>10, 'silver'=>10000, 'gold'=>999999999999); 
  17.  
  18. /** 
  19.  * 状态抽象类 
  20.  * 
  21.  * @author ustb80 
  22.  */ 
  23. abstract class State 
  24.     protected $client
  25.     protected $silver_limit = 10; 
  26.     protected $gold_limit = 10000; 
  27.  
  28.     abstract function deposit($amount);// 存款 
  29.     abstract function withdraw($amount);// 取款 
  30.  
  31.     protected function changeState() 
  32.     { 
  33.         global $config
  34.         if (!empty($config)) 
  35.         { 
  36.             foreach ($config as $key => $val
  37.             { 
  38.                 if ($this->client->balance < $val
  39.                 { 
  40.                     $state_name = ucfirst($key)."State"
  41.                     echo "亲,你已经成为{$key}卡用户\n"
  42.                     $this->client->setState($this->client->$state_name); 
  43.                     break
  44.                 } 
  45.             } 
  46.         } 
  47.     } 
  48.  
  49. // 实现各种乱七八糟的状态 
  50.  
  51. // 新用户 
  52. class NewState extends State 
  53.     public function __construct($client
  54.     { 
  55.         $this->client = $client
  56.     } 
  57.  
  58.     public function deposit($amount
  59.     { 
  60.         $this->client->balance += $amount
  61.         $this->changeState(); 
  62.     } 
  63.  
  64.     public function withdraw($amount
  65.     { 
  66.         die("亲,你还没存钱呢就想着取钱呀,门儿都没有!\n"); 
  67.     } 
  68.  
  69. // 银卡用户 
  70. class SilverState extends State 
  71.     public function __construct($client
  72.     { 
  73.         $this->client = $client
  74.     } 
  75.  
  76.     public function deposit($amount
  77.     { 
  78.         $this->client->balance += $amount
  79.         $this->changeState(); 
  80.     } 
  81.  
  82.     public function withdraw($amount
  83.     { 
  84.         if ($this->balance < $amount
  85.         { 
  86.             die("亲,余额不足\n"); 
  87.         } 
  88.  
  89.         $this->client->balance -= $amount
  90.         $this->changeState(); 
  91.     } 
  92.  
  93. // 金卡用户 
  94. class GoldState extends State 
  95.     public function __construct($client
  96.     { 
  97.         $this->client = $client
  98.     } 
  99.  
  100.     public function deposit($amount
  101.     { 
  102.         $this->balance += $amount
  103.     } 
  104.  
  105.     public function withdraw($amount
  106.     { 
  107.         if ($this->client->balance < $amount
  108.         { 
  109.             die("亲,余额不足\n"); 
  110.         } 
  111.  
  112.         $this->client->balance -= $amount
  113.         $this->changeState(); 
  114.     } 
  115.  
  116.  
  117. // ------------------------------------------------------------- 
  118.  
  119. /** 
  120.  * 银行客户类 
  121.  * 
  122.  * @author ustb80 
  123.  */ 
  124. class BankClient 
  125.     public $balance
  126.     private $state
  127.     public function __construct() 
  128.     { 
  129.         global $config
  130.         foreach ($config as $key => $val
  131.         { 
  132.             $method_name = ucfirst($key)."State"
  133.             $this->$method_name = new $method_name($this); 
  134.         } 
  135.  
  136.         // 初始化状态 
  137.         $this->state = $this->NewState; 
  138.     } 
  139.     /** 
  140.      * 存钱 
  141.      * 
  142.      * @param float $amount 存入金额 
  143.      */ 
  144.     public function deposit($amount
  145.     { 
  146.         $this->state->deposit($amount); 
  147.     } 
  148.  
  149.     /** 
  150.      * 取钱 
  151.      * 
  152.      * @param float $amount 取出金额 
  153.      */ 
  154.     public function withdraw($amount
  155.     { 
  156.         $this->state->withdraw($amount); 
  157.     } 
  158.  
  159.     /** 
  160.      * 变更状态 
  161.      * 
  162.      * @param object $state 
  163.      */ 
  164.     public function setState($state
  165.     { 
  166.         $this->state = $state
  167.     } 
  168.  
  169.     /** 
  170.      * 查看余额 
  171.      */ 
  172.     public function getBalance() 
  173.     { 
  174.         return $this->balance; 
  175.     } 
  176.  
  177. // 测试代码 
  178. $BankClient = new BankClient(); 
  179. $BankClient->deposit(10000); 
  180. $BankClient->withdraw(5000); 
  181. $BankClient->deposit(100000); 
  182.  
  183. // 查看余额 
  184. echo $BankClient->getBalance(); 

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

输出结果:

 
 
  1. 亲,你已经成为gold卡用户 
  2. 亲,你已经成为silver卡用户 
  3. 亲,你已经成为gold卡用户 
  4. 105000 









本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1047086,如需转载请自行联系原作者
目录
相关文章
|
2天前
|
设计模式 JavaScript Java
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
|
4月前
|
设计模式 Java 开发者
Java设计模式【二十一】:状态模式
Java设计模式【二十一】:状态模式
23 0
|
18天前
|
设计模式 Java
23种设计模式,状态模式的概念优缺点以及JAVA代码举例
【4月更文挑战第9天】状态模式是一种行为设计模式,允许一个对象在其内部状态改变时改变它的行为,这个对象看起来似乎修改了它的类。
29 4
|
1月前
|
设计模式
【设计模式】状态模式
【设计模式】状态模式
|
2月前
|
设计模式 Java 测试技术
浅谈设计模式 - 状态模式(十三)
浅谈设计模式 - 状态模式(十三)
17 0
|
3月前
|
设计模式 程序员
设计模式-状态模式(State)
设计模式-状态模式(State)
38 0
|
3月前
|
设计模式 Java
聊聊Java设计模式-状态模式
状态模式(State Pattern)指允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。
52 1
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——14状态模式
Golang设计模式——14状态模式
25 0
|
3月前
|
设计模式 前端开发
【设计模式】之状态模式
状态模式是一种非常有用的设计模式,在前端开发中经常用于管理应用状态和处理复杂的流程。它通过将对象行为和属性与特定条件下的处理逻辑分离开来,提高了代码的可维护性和可扩展性。通过使用状态模式,我们可以优雅地管理应用状态,并根据不同的条件来改变对象行为。然而,在应用状态模式时需要权衡其带来的优缺点,并根据具体情况进行选择。
39 1
|
4月前
|
设计模式
二十三种设计模式全面解析-深入探讨状态模式的高级应用技术:释放对象行为的无限可能
二十三种设计模式全面解析-深入探讨状态模式的高级应用技术:释放对象行为的无限可能