状态模式与策略模式很像,真的很像。
下面举个例子来说明,我们都知道银行经常将用户划分个三六九等,划分的方法很简单,就是用户的存款。下面用php代码来模拟下这个过程:
- <?php
- /**
- * 状态模式的例子
- *
- * Copyright(c) 2012 by ustb80. All rights reserved
- *
- * To contact the author write to {@link mailto:ustb80@163.com}
- *
- * @author ustb80
- * @version $Id: State.php,v 1.0 2012-11-01
- * @package
- */
- // ------------------------------------------------------------------------
- $config = array('new'=>10, 'silver'=>10000, 'gold'=>999999999999);
- /**
- * 状态抽象类
- *
- * @author ustb80
- */
- abstract class State
- {
- protected $client;
- protected $silver_limit = 10;
- protected $gold_limit = 10000;
- abstract function deposit($amount);// 存款
- abstract function withdraw($amount);// 取款
- protected function changeState()
- {
- global $config;
- if (!empty($config))
- {
- foreach ($config as $key => $val)
- {
- if ($this->client->balance < $val)
- {
- $state_name = ucfirst($key)."State";
- echo "亲,你已经成为{$key}卡用户\n";
- $this->client->setState($this->client->$state_name);
- break;
- }
- }
- }
- }
- }
- // 实现各种乱七八糟的状态
- // 新用户
- class NewState extends State
- {
- public function __construct($client)
- {
- $this->client = $client;
- }
- public function deposit($amount)
- {
- $this->client->balance += $amount;
- $this->changeState();
- }
- public function withdraw($amount)
- {
- die("亲,你还没存钱呢就想着取钱呀,门儿都没有!\n");
- }
- }
- // 银卡用户
- class SilverState extends State
- {
- public function __construct($client)
- {
- $this->client = $client;
- }
- public function deposit($amount)
- {
- $this->client->balance += $amount;
- $this->changeState();
- }
- public function withdraw($amount)
- {
- if ($this->balance < $amount)
- {
- die("亲,余额不足\n");
- }
- $this->client->balance -= $amount;
- $this->changeState();
- }
- }
- // 金卡用户
- class GoldState extends State
- {
- public function __construct($client)
- {
- $this->client = $client;
- }
- public function deposit($amount)
- {
- $this->balance += $amount;
- }
- public function withdraw($amount)
- {
- if ($this->client->balance < $amount)
- {
- die("亲,余额不足\n");
- }
- $this->client->balance -= $amount;
- $this->changeState();
- }
- }
- // -------------------------------------------------------------
- /**
- * 银行客户类
- *
- * @author ustb80
- */
- class BankClient
- {
- public $balance;
- private $state;
- public function __construct()
- {
- global $config;
- foreach ($config as $key => $val)
- {
- $method_name = ucfirst($key)."State";
- $this->$method_name = new $method_name($this);
- }
- // 初始化状态
- $this->state = $this->NewState;
- }
- /**
- * 存钱
- *
- * @param float $amount 存入金额
- */
- public function deposit($amount)
- {
- $this->state->deposit($amount);
- }
- /**
- * 取钱
- *
- * @param float $amount 取出金额
- */
- public function withdraw($amount)
- {
- $this->state->withdraw($amount);
- }
- /**
- * 变更状态
- *
- * @param object $state
- */
- public function setState($state)
- {
- $this->state = $state;
- }
- /**
- * 查看余额
- */
- public function getBalance()
- {
- return $this->balance;
- }
- }
- // 测试代码
- $BankClient = new BankClient();
- $BankClient->deposit(10000);
- $BankClient->withdraw(5000);
- $BankClient->deposit(100000);
- // 查看余额
- echo $BankClient->getBalance();
上面用了一个配置数组来动态创建对象实例。
输出结果:
- 亲,你已经成为gold卡用户
- 亲,你已经成为silver卡用户
- 亲,你已经成为gold卡用户
- 105000
本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1047086,如需转载请自行联系原作者