设计模式学习笔记(6) - 状态模式
状态模式与策略模式很像,真的很像。
下面举个例子来说明,我们都知道银行经常将用户划分个三六九等,划分的方法很简单,就是用户的存款。下面用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
设计模式学习笔记(6) - 状态模式相关推荐
- 设计模式学习笔记——备忘录(Memento)模式
设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...
- 设计模式学习笔记——观察者(Observer)模式
设计模式学习笔记--观察者(Observer)模式 @(设计模式)[设计模式, 观察者模式, Observer] 设计模式学习笔记观察者Observer模式 基本介绍 观察者案例 类图 实现代码 Ob ...
- 设计模式学习笔记——访问者(Visitor)模式
设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...
- 设计模式学习笔记——解释器(Interpreter)模式
设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...
- 设计模式学习笔记——命令(Command)模式
设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...
- 设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...
- 设计模式学习笔记——外观(Facade)模式
设计模式学习笔记--外观(Facade)模式 @(设计模式)[设计模式, 外观模式, facade] 设计模式学习笔记外观Facade模式 基本介绍 外观案例 类图 实现代码 Database类 ma ...
- 设计模式学习笔记——装饰(Decorator)模式
设计模式学习笔记--装饰(Decorator)模式 @(设计模式)[设计模式, 装饰模式, decorator] 设计模式学习笔记装饰Decorator模式 基本介绍 装饰案例 类图 实现代码 Dis ...
- 设计模式学习笔记——组合(Composite)模式
设计模式学习笔记--组合(Composite)模式 @(设计模式)[设计模式, 组合模式, composite] 设计模式学习笔记组合Composite模式 基本介绍 组合案例 类图 实现代码 Ent ...
最新文章
- 奋斗---论门当户对
- 伪共享(false sharing),并发编程无声的性能杀手
- oracle修改某个表的字段顺序
- java并发:interrupt进程终止
- 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换
- nginx系列11:负载均衡哈希算法ip_hash与hash模块
- mysql中DateTime、Date、Time、TimeStamp区别
- 统计学怎么求加权指数_统计学——指数
- 伍德里奇计量经济学第三章课后计算机作业,伍德里奇---计量经济学第7章部分计算机习题详解(STATA)...
- vs2008 sp1补丁安装到最后一点点的时候,就无法安装下去了 解决方法[转]
- IDEA+EmmyLua Lua开发环境搭建
- 图片轮播插件slideBox
- 【css练习】方格背景
- Uri.parse()的各种用法
- Vuex入门及进阶笔记
- 招聘信息薪资范围是12-20K,能否要20K的薪资?
- 计算机在化学中的应用总结感悟,计算机在化学中的应用实践总结报告
- 13 | 关于写简历,必须要注意的一些细节
- Blender着色器节点教程 —— AO
- Android程序员需掌握的JVM知识
热门文章
- SpringMVC json/xml自动转换
- 以太坊是什么鬼?!媲美比特币的加密币大揭秘
- 9.6-9.7 awk
- 阿里百川IMSDK--自定义群聊界面
- java thread与runnable联系区别
- python中时间的加n和减n运算
- 使用json 和jQuery制作级联dropdownlist
- LuaTinker向Linux移植成功
- wlnmp+nginx+mysql+php集合包_LNMP(Linux+Nginx+MySQL+PHP)部署详解(一)
- Android 自定义UI--电池