分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

php状态设计模式

发布时间:2023-09-06 02:16责任编辑:蔡小小关键词:暂无标签

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

下面演示了一个简单的状态设计模式,状态设计模式的核心在于47-49行:

 ?1 <?php ?2 ??3 interface Status ?4 { ?5 ????function getProperty(); ?6 ????function getContext(); ?7 } ?8 ??9 abstract class BaseStatus implements Status 10 { 11 ????public $context; 12 ?13 ????public function __construct(BaseContext $context) 14 ????{ 15 ????????$this->context = $context; 16 ????} 17 ?18 ????public function getContext() 19 ????{ 20 ????????return $this->context; 21 ????} 22 } 23 ?24 abstract class BaseContext 25 { 26 ????public $currentStatus; 27 ?28 ????public function changeStatus(Status $status) 29 ????{ 30 ????????$this->currentStatus = $status; 31 ????} 32 ?33 ????public function statusDetail() 34 ????{ 35 ????????return $this->currentStatus->getProperty(); 36 ????} 37 } 38 ?39 class Context extends BaseContext 40 { 41 ????public $status1; 42 ????public $status2; 43 ????public $status3; 44 ?45 ????public function __construct() 46 ????{ 47 ????????$this->status1 = new Status1($this); 48 ????????$this->status2 = new Status2($this); 49 ????????$this->status3 = new Status3($this); 50 ?51 ????????$this->currentStatus = $this->status1; 52 ????} 53 } 54 ?55 class Status1 extends BaseStatus 56 { 57 ????public function getProperty() 58 ????{ 59 ????????echo ‘I am status1‘.PHP_EOL; 60 ????????if ($this->getContext()->currentStatus instanceof self) { 61 ????????????echo ‘here is status two and status three<br/>‘; 62 ????????????$this->context->status2->getProperty(); 63 ????????????echo ‘<br/>‘; 64 ????????????$this->context->status3->getProperty(); 65 ????????????echo ‘<br/>‘; 66 ????????} 67 ????} 68 } 69 ?70 class Status2 extends BaseStatus 71 { 72 ????public function getProperty() 73 ????{ 74 ????????echo ‘I am status2‘.PHP_EOL; 75 ????????if ($this->getContext()->currentStatus instanceof self) { 76 ????????????echo ‘here is status one and status three<br/>‘; 77 ????????????$this->context->status1->getProperty(); 78 ????????????echo ‘<br/>‘; 79 ????????????$this->context->status3->getProperty(); 80 ????????????echo ‘<br/>‘; 81 ????????} 82 ????} 83 } 84 ?85 class Status3 extends BaseStatus 86 { 87 ????public function getProperty() 88 ????{ 89 ????????echo ‘I am status3‘.PHP_EOL; 90 ????????if ($this->getContext()->currentStatus instanceof self) { 91 ????????????echo ‘here is status one and status two<br/>‘; 92 ????????????$this->context->status1->getProperty(); 93 ????????????echo ‘<br/>‘; 94 ????????????$this->context->status2->getProperty(); 95 ????????????echo ‘<br/>‘; 96 ????????} 97 ????} 98 } 99 100 $context = new Context();101 102 $context->statusDetail();103 104 $context->changeStatus($context->status2);105 106 $context->statusDetail();107 108 $context->changeStatus($context->status3);109 110 $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状态设计模式

原文地址:https://www.cnblogs.com/SHQHDMR/p/9721474.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved