状态设计模式的关键就是,环境中拥有所需的全部状态对象,每个状态对象又引用了环境对象;环境对象通过维护一个当前状态属性(用于存放状态对象)从而对所需的全部状态对象产生影响。

下面演示了一个简单的状态设计模式,状态设计模式的核心在于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状态设计模式的更多相关文章

  1. State状态设计模式

    1.状态模式:改变对象的行为 一个用来改变类的(状态的)对象. 2:问题:当你自己实现 State 模式的时候就会碰到很多细节的问题,你必须根据自己的需要选择合适的实现方法, 比如用到的状态(Stat ...

  2. State Design Pattern 状态设计模式

    设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换. 有点像Finite Automata算法,两者的思想是一样的. 会Finite Automata,那么这个设计模式就非常easy了. # ...

  3. 使用C# (.NET Core) 实现状态设计模式 (State Pattern)

    本文的概念性内容来自深入浅出设计模式一书 项目需求 这是一个糖果机的需求图. 它有四种状态, 分别是图中的四个圆圈: No Quarter: 无硬币 Has Quater 有硬币 Gumball So ...

  4. State模式(状态设计模式)

    State??? State模式中,我们用类来表示状态.以类来表示状态后,我们就能通过切换类来方便地改变对象的状态.当需要增加新的状态时,如何修改代码这个问题也会很明确. 直接用状态代替硬编码 依赖于 ...

  5. python设计模式之状态模式

    python设计模式之状态模式 面向对象编程着力于在对象交互时改变它们的状态.在很多问题中,有限状态机(通常名为状态机)是一个非常方便的状态转换建模(并在必要时以数学方式形式化)工具.首先,什么是状态 ...

  6. 深入探索Java设计模式(二)之策略模式

    策略设计模式是Java API库中常见的模式之一.这与另一个设计模式(称为状态设计模式)非常相似.本文是在学习完优锐课JAVA架构VIP课程—[框架源码专题]中<学习源码中的优秀设计模式> ...

  7. 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 ...

  8. C#设计模式:状态者模式(State Pattern)

    一,什么是状态设计模式? 1,定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 2,当一个对象的内部状态改变时允许改变其行为,这个对象看起来像是 ...

  9. 第18章 备忘录模式(Memento Pattern)

    原文  第18章 备忘录模式(Memento Pattern) 备忘录模式       概述: 备忘录模式(Memento Pattern)又叫做快照模式(Snapshot Pattern)或Toke ...

随机推荐

  1. day 10 函数入门

    def login(): """ """ 执行函数不会报错

  2. X分钟速成Y (其中Y=Python3)

    # 用井字符开头的是单行注释 """ 多行字符串用三个引号 包裹,也常被用来做多 行注释 """ ##################### ...

  3. 使用元类 编写ORM

    元类 一句话: 元类定制类的创建行为 知识点 1.类的创建: python这种动态语言,函数和类的定义,不是编译时定义的,而是运行时动态创建的. Python解释器遇到class定义时,仅仅是扫描一下 ...

  4. IDEA 类图功能使用方法

    1. Ctrl+Shift+Alt+U显示类图,(可以选中代码中类,再按快捷键,直接进入此类的类图) 2. 在类图中,选中某类右击显示Show Implementations,弹出子类的选择框. 按S ...

  5. jq实时监测输入框内容改变

    $(document) .on('input propertychange','#telInput',function (e) { if (e.type === "input" | ...

  6. Parsing Natural Scenes and Natural Language with Recursive Neural Networks-paper

    Parsing Natural Scenes and Natural Language with Recursive Neural Networks作者信息: Richard Socher richa ...

  7. PHP批量生成手机号

    <?php //匹配手机号的正则表达式 #^(13[0-9]|14[47]|15[0-35-9]|17[6-8]|18[0-9])([0-9]{8})$# $arr = array( 130,1 ...

  8. 2018-2019-2 20165212《网络对抗技术》Exp2 后门原理与实践

    2018-2019-2 20165212<网络对抗技术>Exp2 后门原理与实践 1.实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作 ...

  9. c# 数据存储过程

    什么是存储过程? 用某百科的话来说就是一堆为了完成某一功能或者某些功能的SQL语句的集合,而数据库则会将这些存储过程的方法存储到数据库中去. 优点: 1.可重用并且效率高:存储过程经过一次编译后不需要 ...

  10. 【剑指offer】求树中满足和为给定数字的路径

    题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大的 ...