您的位置:首页 > 其它

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

2012-11-01 18:06 337 查看
状态模式与策略模式很像,真的很像。

下面举个例子来说明,我们都知道银行经常将用户划分个三六九等,划分的方法很简单,就是用户的存款。下面用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


本文出自 “凡星的技术博客” 博客,请务必保留此出处http://ustb80.blog.51cto.com/6139482/1047086
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: