php状态设计模式
状态设计模式的关键就是,环境中拥有所需的全部状态对象,每个状态对象又引用了环境对象;环境对象通过维护一个当前状态属性(用于存放状态对象)从而对所需的全部状态对象产生影响。
下面演示了一个简单的状态设计模式,状态设计模式的核心在于47-49行:
<?php interface Status
{
function getProperty();
function getContext();
} abstract class BaseStatus implements Status
{
public $context; public function __construct(BaseContext $context)
{
$this->context = $context;
} public function getContext()
{
return $this->context;
}
} abstract class BaseContext
{
public $currentStatus; public function changeStatus(Status $status)
{
$this->currentStatus = $status;
} public function statusDetail()
{
return $this->currentStatus->getProperty();
}
} class Context extends BaseContext
{
public $status1;
public $status2;
public $status3; public function __construct()
{
$this->status1 = new Status1($this);
$this->status2 = new Status2($this);
$this->status3 = new Status3($this); $this->currentStatus = $this->status1;
}
} class Status1 extends BaseStatus
{
public function getProperty()
{
echo 'I am status1'.PHP_EOL;
if ($this->getContext()->currentStatus instanceof self) {
echo 'here is status two and status three<br/>';
$this->context->status2->getProperty();
echo '<br/>';
$this->context->status3->getProperty();
echo '<br/>';
}
}
} class Status2 extends BaseStatus
{
public function getProperty()
{
echo 'I am status2'.PHP_EOL;
if ($this->getContext()->currentStatus instanceof self) {
echo 'here is status one and status three<br/>';
$this->context->status1->getProperty();
echo '<br/>';
$this->context->status3->getProperty();
echo '<br/>';
}
}
} class Status3 extends BaseStatus
{
public function getProperty()
{
echo 'I am status3'.PHP_EOL;
if ($this->getContext()->currentStatus instanceof self) {
echo 'here is status one and status two<br/>';
$this->context->status1->getProperty();
echo '<br/>';
$this->context->status2->getProperty();
echo '<br/>';
}
}
} $context = new Context(); $context->statusDetail(); $context->changeStatus($context->status2); $context->statusDetail(); $context->changeStatus($context->status3); $context->statusDetail();
运行结果:
I am status1 here is status two and status three
I am status2
I am status3
I am status2 here is status one and status three
I am status1
I am status3
I am status3 here is status one and status two
I am status1
I am status2
可见,每次环境对象切换状态的时候,环境对象内的任何修改对于所有状态对象都是同步可见的(状态对象均引用了环境对象)。
php状态设计模式的更多相关文章
- State状态设计模式
1.状态模式:改变对象的行为 一个用来改变类的(状态的)对象. 2:问题:当你自己实现 State 模式的时候就会碰到很多细节的问题,你必须根据自己的需要选择合适的实现方法, 比如用到的状态(Stat ...
- State Design Pattern 状态设计模式
设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换. 有点像Finite Automata算法,两者的思想是一样的. 会Finite Automata,那么这个设计模式就非常easy了. # ...
- 使用C# (.NET Core) 实现状态设计模式 (State Pattern)
本文的概念性内容来自深入浅出设计模式一书 项目需求 这是一个糖果机的需求图. 它有四种状态, 分别是图中的四个圆圈: No Quarter: 无硬币 Has Quater 有硬币 Gumball So ...
- State模式(状态设计模式)
State??? State模式中,我们用类来表示状态.以类来表示状态后,我们就能通过切换类来方便地改变对象的状态.当需要增加新的状态时,如何修改代码这个问题也会很明确. 直接用状态代替硬编码 依赖于 ...
- python设计模式之状态模式
python设计模式之状态模式 面向对象编程着力于在对象交互时改变它们的状态.在很多问题中,有限状态机(通常名为状态机)是一个非常方便的状态转换建模(并在必要时以数学方式形式化)工具.首先,什么是状态 ...
- 深入探索Java设计模式(二)之策略模式
策略设计模式是Java API库中常见的模式之一.这与另一个设计模式(称为状态设计模式)非常相似.本文是在学习完优锐课JAVA架构VIP课程—[框架源码专题]中<学习源码中的优秀设计模式> ...
- python设计模式第2版
python设计模式第2版 目录 第1章 设计模式简介 1 1.1 理解面向对象编程 1 1.1.1 对象 2 1.1.2 类 2 1.1.3 方法 2 1.2 面向对象编程的主要概念 3 1.2.1 ...
- C#设计模式:状态者模式(State Pattern)
一,什么是状态设计模式? 1,定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 2,当一个对象的内部状态改变时允许改变其行为,这个对象看起来像是 ...
- 第18章 备忘录模式(Memento Pattern)
原文 第18章 备忘录模式(Memento Pattern) 备忘录模式 概述: 备忘录模式(Memento Pattern)又叫做快照模式(Snapshot Pattern)或Toke ...
随机推荐
- 解决android 9上无法使用http协议
用户反应本来好用的app,突然无法访问服务器,不能正常用了,拿到手机,从头检查权限,重新安装都不能解决,网络是正常的,怎么就不能访问网络了呢?所有想到的办法都用了而不能解决,最后想起看一下androi ...
- FCC JS基础算法题(12):Where do I belong(数组排序并找出元素索引)
题目描述: 先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引.举例:where([1,2,3,4], 1.5) 应该返回 1.因为1.5插入到数组[1,2,3,4]后变成[1,1.5 ...
- 入门项目 A4 db_handler 数据操作文件
''' 数据处理层 ''' from conf import settings # 以下代码中有提前定义的路径函数,需要导入配置文件包下面的设置模块 import json # 以下代码中有需要序列化 ...
- JAVA高级篇(四、JVM垃圾回收和调优)
本文转自https://zhuanlan.zhihu.com/p/25539690 JVM GC(垃圾回收机制) 在学习Java GC 之前,我们需要记住一个单词:stop-the-world .它会 ...
- annotation的概念及其作用
概念 能够添加到 Java 源代码的语法元数据.类.方法.变量.参数.包都可以被注解,可用来将信息元数据与程序元素进行关联.Annotation 中文常译为“注解”. 作用 标记,用于告诉编译器一些信 ...
- LeetCode - Online Election
In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...
- 根据CPU核心数确定线程池并发线程数
一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...
- Linux tmpwatch命令详解
Linux tmpwatch命令 Linux tmpwatch命令用于删除暂存文件. 执行tmpwatch指令可删除不必要的暂存文件,您可以设置文件超期时间,单位以小时计算 用法: tmpwatch ...
- Spark流处理调优步骤
总体运行状况: 这里的每个批处理任务间隔是10s一次,所以Total Delay是14s,那么对于下一个批处理任务来说就是延迟了14 - 10 = 4s . Total Delay - 每个批处 ...
- 使用phpAnalysis打造PHP应用非侵入式性能分析器
使用phpAnalysis打造PHP应用非侵入式性能分析器,查找PHP性能瓶颈. 什么是phpAnalysis phpAnalysis是一款轻量级非侵入式PHP应用性能分析器,适用于开发.测试及生产环 ...