0

0

PHP设计模式之状态模式定义与用法

不言

不言

发布时间:2018-04-09 16:43:23

|

1601人浏览过

|

来源于php中文网

原创

这篇文章主要给大家介绍了php设计模式之状态模式定义与用法,有需要的朋友可以参考一下

本文实例讲述了PHP设计模式之状态模式定义与用法。分享给大家供大家参考,具体如下:

什么是状态设计模式

当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化。

立即学习PHP免费学习笔记(深入)”;

什么时候使用状态模式

对象中频繁改变非常依赖于条件语句。 就其自身来说, 条件语句本身没有什么问题(如switch语句或带else子句的语句),不过, 如果选项太多, 以到程序开始出现混乱, 或者增加或改变选项需要花费太多时间, 甚至成为一种负担, 这就出现了问题

对于状态设计模式, 每个状态都有自己的具体类, 它们实现一个公共接口. 我们不用查看对象的控制流, 而是从另一个角度来考虑, 即对象的状态.

状态机是一个模型, 其重点包括不同的状态, 一个状态到另一个状态的变迁, 以及导致状态改变的触发器.

以开灯关灯为例子, 状态模型的本质分为3点:

①状态(关灯和开灯)
②变迁(从关灯到开灯, 以及从开灯到关灯)
③触发器(灯开关)

所以状态模式都需要一个参与者来跟踪对象所处的状态. 以Light为例, Light需要知道当前状态是什么.

示例:开灯关灯

Light.php

offState = new OffState($this);
    $this->onState = new OnState($this);
    //开始状态为关闭状态Off
    $this->currentState = $this->offState;
  }
  //调用状态方法触发器
  public function turnLightOn()
  {
    $this->currentState->turnLightOn();
  }
  public function turnLightOff()
  {
    $this->currentState->turnLightOff();
  }
  //设置当前状态
  public function setState(IState $state)
  {
    $this->currentState = $state;
  }
  //获取状态
  public function getOnState()
  {
    return $this->onState;
  }
  public function getOffState()
  {
    return $this->offState;
  }
}

在构造函数中, Light实例化IState实现的两个实例-----一个对应关, 一个对应开

$this->offState = new OffState($this);
$this->onState = new OnState($this);

这个实例化过程用到了一种递归, 称为自引用(self-referral)

构造函数参数中的实参写为$this, 这是Light类自身的一个引用. 状态类希望接收一个Light类实例做参数,.

setState方法是为了设置一个当前状态 需要一个状态对象作为实参, 一旦触发一个状态, 这个状态就会向Light类发送信息, 指定当前状态.

状态实例

IState接口

IState.php

该接口的实现类

OnState.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "灯已经打开了->不做操作
"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!
"; $this->light->setState($this->light->getOffState()); } }

OffState.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "灯打开!可以看见帅哥chenqionghe了!
"; $this->light->setState($this->light->getOnState()); } public function turnLightOff() { echo "灯已经关闭了->不做操作
"; } }

默认状态是OffState, 它必须实现IState方法turnLightOn和turnLightOff, Light调用turnLightOn方法, 会显示(灯打开!可以看见帅哥chenqionghe了), 然后将OnState设置为当前状态, 不过,如果是调用 OffState的turnLightOff方法, 就只有提示灯已经被关闭了 不会有其他动作.

客户

Client的所有请求都是通过Light发出, Client和任何状态类之间都没有直接连接, 包括IState接口.下面的Client显示了触发两个状态中所有方法的请求.

Client.php

light = new Light();
    $this->light->turnLightOn();
    $this->light->turnLightOn();
    $this->light->turnLightOff();
    $this->light->turnLightOff();
  }
}
$worker = new Client();

增加状态

对于所有的设计模式来说,很重要的一个方面是: 利用这些设计模式可以很容易地做出修改. 与其他模式一样,状态模式也很易于更新和改变. 下面在这个灯的示例上再加两个状态:更亮(Brighter)和最亮(Brightest)

现在变成了4个状态, 序列有所改变. '关'(off)状态只能变到"开"(on)状态, on状态不能变到off状态. on状态只能变到"更亮"(brighter)状态和"最亮"(brightest)状态. 只能最亮状态才可能变到关状态.

改变接口

要改变的第一个参与者是接口IState, 这个接口中必须指定相应的方法, 可以用来迁移到brighter和brightest状态.

IState.php

现在所有状态类都必须包含这4个方法, 它们都需要结合到Light类中.

改变状态

状态设计模式中有改变时, 这些新增的改变会对模式整体的其他方面带来影响. 不过, 增加改变相当简单, 每个状态只有一个特定的变迁.

四个状态

OnState.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "不合法的操作!
"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!
"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "灯更亮了, 看帅哥chenqionghe看得更真切了!
"; $this->light->setState($this->light->getBrighterState()); } public function turnBrightest() { echo "不合法的操作!
"; } }

OffState.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "灯打开!可以看见帅哥chenqionghe了!
"; $this->light->setState($this->light->getOnState()); } public function turnLightOff() { echo "不合法的操作!
"; } public function turnBrighter() { echo "不合法的操作!
"; } public function turnBrightest() { echo "不合法的操作!
"; } }

Brighter.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "不合法的操作!
"; } public function turnLightOff() { echo "不合法的操作!
"; } public function turnBrighter() { echo "不合法的操作!
"; } public function turnBrightest() { echo "灯最亮了, 看帅哥chenqionghe已经帅到无敌!
"; $this->light->setState($this->light->getBrightestState()); } }

Brightest.php

light = $light;
  }
  public function turnLightOn()
  {
    echo "灯已经打开了->不做操作
"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!
"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "不合法的操作!
"; } public function turnBrightest() { echo "不合法的操作!
"; } }

更新Light类

ShopWind网店系统
ShopWind网店系统

ShopWind网店系统是国内最专业的网店程序之一,采用ASP语言设计开发,速度快、性能好、安全性高。ShopWind网店购物系统提供性化的后台管理界面,标准的网上商店管理模式和强大的网店软件后台管理功能。ShopWind网店系统提供了灵活强大的模板机制,内置多套免费精美模板,同时可在后台任意更换,让您即刻快速建立不同的网店外观。同时您可以对网模板自定义设计,建立个性化网店形象。ShopWind网

下载

Light.php

offState = new OffState($this);
    $this->onState = new OnState($this);
    $this->brighterState = new BrighterState($this);
    $this->brightestState = new BrightestState($this);
    //开始状态为关闭状态Off
    $this->currentState = $this->offState;
  }
  //调用状态方法触发器
  public function turnLightOn()
  {
    $this->currentState->turnLightOn();
  }
  public function turnLightOff()
  {
    $this->currentState->turnLightOff();
  }
  public function turnLightBrighter()
  {
    $this->currentState->turnBrighter();
  }
  public function turnLigthBrightest()
  {
    $this->currentState->turnBrightest();
  }
  //设置当前状态
  public function setState(IState $state)
  {
    $this->currentState = $state;
  }
  //获取状态
  public function getOnState()
  {
    return $this->onState;
  }
  public function getOffState()
  {
    return $this->offState;
  }
  public function getBrighterState()
  {
    return $this->brighterState;
  }
  public function getBrightestState()
  {
    return $this->brightestState;
  }
}

更新客户

light = new Light();
    $this->light->turnLightOn();
    $this->light->turnLightBrighter();
    $this->light->turnLigthBrightest();
    $this->light->turnLightOff();
    $this->light->turnLigthBrightest();
  }
}
$worker = new Client();

运行结果如下

灯打开!可以看见帅哥chenqionghe了!灯更亮了, 看帅哥chenqionghe看得更真切了!灯最亮了, 看帅哥chenqionghe已经帅到无敌!灯关闭!看不见帅哥chenqionghe了!不合法的操作!

九宫格移动示例

九宫格的移动分为4个移动:

上(Up)
下(Down)
左(Left)
右(Right)

对于这些移动,规则是要求单元格之间不能沿对角线方向移动. 另外, 从一个单元格移动到下一个单元格时, 一次只能移动一个单元格

要使用状态设计模式来建立一个九宫格移动示例,

建立接口

IMatrix.php

虽然这个状态设计模式有9个状态, 分别对应九个单元格, 但一个状态最多只需要4个变迁

上下文

对于状态中的4个变迁或移动方法, 上下文必须提供相应方法来调用这些变迁方法, 另外还要完成各个状态的实例化.

Context.php

cell1 = new Cell1State($this);
    $this->cell2 = new Cell2State($this);
    $this->cell3 = new Cell3State($this);
    $this->cell4 = new Cell4State($this);
    $this->cell5 = new Cell5State($this);
    $this->cell6 = new Cell6State($this);
    $this->cell7 = new Cell7State($this);
    $this->cell8 = new Cell8State($this);
    $this->cell9 = new Cell9State($this);
    $this->currentState = $this->cell5;
  }
  //调用方法
  public function doUp()
  {
    $this->currentState->goUp();
  }
  public function doDown()
  {
    $this->currentState->goDown();
  }
  public function doLeft()
  {
    $this->currentState->goLeft();
  }
  public function doRight()
  {
    $this->currentState->goRight();
  }
  //设置当前状态
  public function setState(IMatrix $state)
  {
    $this->currentState = $state;
  }
  //获取状态
  public function getCell1State()
  {
    return $this->cell1;
  }
  public function getCell2State()
  {
    return $this->cell2;
  }
  public function getCell3State()
  {
    return $this->cell3;
  }
  public function getCell4State()
  {
    return $this->cell4;
  }
  public function getCell5State()
  {
    return $this->cell5;
  }
  public function getCell6State()
  {
    return $this->cell6;
  }
  public function getCell7State()
  {
    return $this->cell7;
  }
  public function getCell8State()
  {
    return $this->cell8;
  }
  public function getCell9State()
  {
    return $this->cell9;
  }
}

状态

9个状态表示九宫格中的不同单元格, 为了唯一显示单元格,会分别输出相应到达的单元格数字, 这样能够更清楚地看出穿过矩阵的路线.

Cell1State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '不合法的移动!
'; } public function goRight() { echo '走到2
'; $this->context->setState($this->context->getCell2State()); } public function goUp() { echo '不合法的移动!
'; } public function goDown() { echo '走到4
'; $this->context->setState($this->context->getCell4State()); } }

Cell2State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到1
'; $this->context->setState($this->context->getCell1State()); } public function goRight() { echo '走到3
'; $this->context->setState($this->context->getCell3State()); } public function goUp() { echo '不合法的移动!
'; } public function goDown() { echo '走到5
'; $this->context->setState($this->context->getCell5State()); } }

Cell3State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到2
'; $this->context->setState($this->context->getCell2State()); } public function goRight() { echo '不合法的移动!
'; } public function goUp() { echo '不合法的移动!
'; } public function goDown() { echo '走到6
'; $this->context->setState($this->context->getCell6State()); } }

Cell4State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '不合法的移动!
'; } public function goRight() { echo '走到5
'; $this->context->setState($this->context->getCell5State()); } public function goUp() { echo '走到1
'; $this->context->setState($this->context->getCell1State()); } public function goDown() { echo '走到7
'; $this->context->setState($this->context->getCell7State()); } }

Cell5State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到4
'; $this->context->setState($this->context->getCell4State()); } public function goRight() { echo '走到6
'; $this->context->setState($this->context->getCell6State()); } public function goUp() { echo '走到2
'; $this->context->setState($this->context->getCell2State()); } public function goDown() { echo '走到8
'; $this->context->setState($this->context->getCell8State()); } }

Cell6State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到5
'; $this->context->setState($this->context->getCell5State()); } public function goRight() { echo '不合法的移动!
'; } public function goUp() { echo '走到3
'; $this->context->setState($this->context->getCell3State()); } public function goDown() { echo '走到9
'; $this->context->setState($this->context->getCell9State()); } }

Cell7State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '不合法的移动!
'; } public function goRight() { echo '走到8
'; $this->context->setState($this->context->getCell8State()); } public function goUp() { echo '走到4
'; $this->context->setState($this->context->getCell4State()); } public function goDown() { echo '不合法的移动!
'; } }

Cell8State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到7
'; $this->context->setState($this->context->getCell7State()); } public function goRight() { echo '走到9
'; $this->context->setState($this->context->getCell9State()); } public function goUp() { echo '走到5
'; $this->context->setState($this->context->getCell5State()); } public function goDown() { echo '不合法的移动!
'; } }

Cell9State

context = $contextNow;
  }
  public function goLeft()
  {
    echo '走到8
'; $this->context->setState($this->context->getCell8State()); } public function goRight() { echo '不合法的移动!
'; } public function goUp() { echo '走到6
'; $this->context->setState($this->context->getCell6State()); } public function goDown() { echo '不合法的移动!
'; } }

要想有效地使用状态设计模式, 真正的难点在于要想象现实或模拟世界是怎么样

客户Client

下面从单元格5开始进行一个上,右,下, 下,左,上的移动

Client.php

context = new Context();
    $this->context->doUp();
    $this->context->doRight();
    $this->context->doDown();
    $this->context->doDown();
    $this->context->doLeft();
    $this->context->doUp();
  }
}
$worker = new Client();

运行结果如下

走到2
走到3
走到6
走到9
走到8
走到5

状态模式与PHP

很多人把状态设计模式看做是实现模拟器和游戏的主要方法.总的说来, 这确实是状态模式的目标,不过险些之外, 状态模型(状态引擎)和状态设计模式在PHP中也有很多应用.用PHP完成更大的项目时, 包括Facebook和WordPress, 会有更多的新增特性和当前状态需求.对于这种不断有改变和增长的情况, 就可以采用可扩展的状态模式来管理.

PHP开发人员如何创建包含多个状态的程序, 将决定状态模式的使用范围. 所以不仅状态机在游戏和模拟世界中有很多应用, 实际上状态模型还有更多适用的领域.只要PHP程序的用户会用到一组有限的状态, 开发人员就可以使用状态设计模式.

相关推荐:

php设计模式一之命名空间、自动加载类、PSR-0编码规范

16个PHP设计模式介绍

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
switch语句用法
switch语句用法

switch语句用法:1、Switch语句只能用于整数类型,枚举类型和String类型,不能用于浮点数类型和布尔类型;2、每个case语句后面必须跟着一个break语句,以防止执行其他case的代码块,没有break语句,将会继续执行下一个case的代码块;3、可以在一个case语句中匹配多个值,使用逗号分隔;4、Switch语句中的default代码块是可选的等等。

541

2023.09.21

Java switch的用法
Java switch的用法

Java中的switch语句用于根据不同的条件执行不同的代码块。想了解更多switch的相关内容,可以阅读本专题下面的文章。

423

2024.03.13

硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

1157

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

215

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2031

2025.12.29

java接口相关教程
java接口相关教程

本专题整合了java接口相关内容,阅读专题下面的文章了解更多详细内容。

23

2026.01.19

wordpress seo
wordpress seo

WordPress网站SEO优化方法有:1、选择一个SEO友好的主题,具有清晰的代码结构,快速的加载速度和响应式设计;2、使用SEO插件,优化你的标题标签,元描述,关键字,XML站点地图等;3、优化你的内容,内容是SEO优化的核心;4、优化你的网站速度;5、创建友好的URL;6、使用内部链接;7、优化图像;8、使用社交媒体;9、定期更新你的网站;10、监控和分析你的网站等等。

420

2023.09.18

wordpress下载后怎么安装
wordpress下载后怎么安装

安装前准备:确保服务器满足要求、获取安装文件、创建数据库。上传 wordpress 文件。创建数据库和用户。运行安装程序:选择语言、输入数据库信息、网站标题和管理员信息。安装 wordpress。安装后配置:设置永久链接、安装主题、安装插件、创建内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

317

2024.04.15

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

32

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 5万人学习

Git 教程
Git 教程

共21课时 | 3.2万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号