本文实例讲述了php设计模式之状态模式定义与用法。分享给大家供大家参考,具体如下:
什么是状态设计模式
当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化。
什么时候使用状态模式
对象中频繁改变非常依赖于条件语句。 就其自身来说, 条件语句本身没有什么问题(如switch语句或带else子句的语句),不过, 如果选项太多, 以到程序开始出现混乱, 或者增加或改变选项需要花费太多时间, 甚至成为一种负担, 这就出现了问题
对于状态设计模式, 每个状态都有自己的具体类, 它们实现一个公共接口. 我们不用查看对象的控制流, 而是从另一个角度来考虑, 即对象的状态.
状态机是一个模型, 其重点包括不同的状态, 一个状态到另一个状态的变迁, 以及导致状态改变的触发器.
以开灯关灯为例子, 状态模型的本质分为3点:
①状态(关灯和开灯)
②变迁(从关灯到开灯, 以及从开灯到关灯)
③触发器(灯开关)
所以状态模式都需要一个参与者来跟踪对象所处的状态. 以light为例, light需要知道当前状态是什么.
示例:开灯关灯
light.php
<?php
class light
{
private $offstate; //关闭状态
private $onstate; //开启状态
private $currentstate; //当前状态
public function __construct()
{
$this->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
<?php
interface istate
{
public function turnlighton();
public function turnlightoff();
}
该接口的实现类
onstate.php
<?php
class onstate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "灯已经打开了->不做操作<br />";
}
public function turnlightoff()
{
echo "灯关闭!看不见帅哥chenqionghe了!<br />";
$this->light->setstate($this->light->getoffstate());
}
}
offstate.php
<?php
class offstate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "灯打开!可以看见帅哥chenqionghe了!<br />";
$this->light->setstate($this->light->getonstate());
}
public function turnlightoff()
{
echo "灯已经关闭了->不做操作<br />";
}
}
默认状态是offstate, 它必须实现istate方法turnlighton和turnlightoff, light调用turnlighton方法, 会显示(灯打开!可以看见帅哥chenqionghe了), 然后将onstate设置为当前状态, 不过,如果是调用 offstate的turnlightoff方法, 就只有提示灯已经被关闭了 不会有其他动作.
客户
client的所有请求都是通过light发出, client和任何状态类之间都没有直接连接, 包括istate接口.下面的client显示了触发两个状态中所有方法的请求.
client.php
<?php
function __autoload($class_name)
{
include_once $class_name.'.php';
}
class client
{
private $light;
public function __construct()
{
$this->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
<?php
interface istate
{
public function turnlighton();
public function turnlightoff();
public function turnbrighter();
public function turnbrightest();
}
现在所有状态类都必须包含这4个方法, 它们都需要结合到light类中.
改变状态
状态设计模式中有改变时, 这些新增的改变会对模式整体的其他方面带来影响. 不过, 增加改变相当简单, 每个状态只有一个特定的变迁.
四个状态
onstate.php
<?php
class onstate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "不合法的操作!<br />";
}
public function turnlightoff()
{
echo "灯关闭!看不见帅哥chenqionghe了!<br />";
$this->light->setstate($this->light->getoffstate());
}
public function turnbrighter()
{
echo "灯更亮了, 看帅哥chenqionghe看得更真切了!<br />";
$this->light->setstate($this->light->getbrighterstate());
}
public function turnbrightest()
{
echo "不合法的操作!<br />";
}
}
offstate.php
<?php
class offstate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "灯打开!可以看见帅哥chenqionghe了!<br />";
$this->light->setstate($this->light->getonstate());
}
public function turnlightoff()
{
echo "不合法的操作!<br />";
}
public function turnbrighter()
{
echo "不合法的操作!<br />";
}
public function turnbrightest()
{
echo "不合法的操作!<br />";
}
}
brighter.php
<?php
class brighterstate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "不合法的操作!<br />";
}
public function turnlightoff()
{
echo "不合法的操作!<br />";
}
public function turnbrighter()
{
echo "不合法的操作!<br />";
}
public function turnbrightest()
{
echo "灯最亮了, 看帅哥chenqionghe已经帅到无敌!<br />";
$this->light->setstate($this->light->getbrighteststate());
}
}
brightest.php
<?php
class brighteststate implements istate
{
private $light;
public function __construct(light $light)
{
$this->light = $light;
}
public function turnlighton()
{
echo "灯已经打开了->不做操作<br />";
}
public function turnlightoff()
{
echo "灯关闭!看不见帅哥chenqionghe了!<br />";
$this->light->setstate($this->light->getoffstate());
}
public function turnbrighter()
{
echo "不合法的操作!<br />";
}
public function turnbrightest()
{
echo "不合法的操作!<br />";
}
}
更新light类
light.php
<?php
class light
{
private $offstate; //关闭状态
private $onstate; //开启状态
private $brighterstate; //更亮状态
private $brighteststate;//最亮状态
private $currentstate; //当前状态
public function __construct()
{
$this->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;
}
}
更新客户
<?php
function __autoload($class_name)
{
include_once $class_name.'.php';
}
class client
{
private $light;
public function __construct()
{
$this->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
<?php
interface imatrix
{
public function goup();
public function godown();
public function goleft();
public function goright();
}
虽然这个状态设计模式有9个状态, 分别对应九个单元格, 但一个状态最多只需要4个变迁
上下文
对于状态中的4个变迁或移动方法, 上下文必须提供相应方法来调用这些变迁方法, 另外还要完成各个状态的实例化.
context.php
<?php
class context
{
private $cell1;
private $cell2;
private $cell3;
private $cell4;
private $cell5;
private $cell6;
private $cell7;
private $cell8;
private $cell9;
private $currentstate;
public function __construct()
{
$this->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
<?php
class cell1state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '不合法的移动!<br />';
}
public function goright()
{
echo '走到<strong>2</strong><br />';
$this->context->setstate($this->context->getcell2state());
}
public function goup()
{
echo '不合法的移动!<br />';
}
public function godown()
{
echo '走到<strong>4</strong><br />';
$this->context->setstate($this->context->getcell4state());
}
}
cell2state
<?php
class cell2state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>1</strong><br />';
$this->context->setstate($this->context->getcell1state());
}
public function goright()
{
echo '走到<strong>3</strong><br />';
$this->context->setstate($this->context->getcell3state());
}
public function goup()
{
echo '不合法的移动!<br />';
}
public function godown()
{
echo '走到<strong>5</strong><br />';
$this->context->setstate($this->context->getcell5state());
}
}
cell3state
<?php
class cell3state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>2</strong><br />';
$this->context->setstate($this->context->getcell2state());
}
public function goright()
{
echo '不合法的移动!<br />';
}
public function goup()
{
echo '不合法的移动!<br />';
}
public function godown()
{
echo '走到<strong>6</strong><br />';
$this->context->setstate($this->context->getcell6state());
}
}
cell4state
<?php
class cell4state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '不合法的移动!<br />';
}
public function goright()
{
echo '走到<strong>5</strong><br />';
$this->context->setstate($this->context->getcell5state());
}
public function goup()
{
echo '走到<strong>1</strong><br />';
$this->context->setstate($this->context->getcell1state());
}
public function godown()
{
echo '走到<strong>7</strong><br />';
$this->context->setstate($this->context->getcell7state());
}
}
cell5state
<?php
class cell5state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>4</strong><br />';
$this->context->setstate($this->context->getcell4state());
}
public function goright()
{
echo '走到<strong>6</strong><br />';
$this->context->setstate($this->context->getcell6state());
}
public function goup()
{
echo '走到<strong>2</strong><br />';
$this->context->setstate($this->context->getcell2state());
}
public function godown()
{
echo '走到<strong>8</strong><br />';
$this->context->setstate($this->context->getcell8state());
}
}
cell6state
<?php
class cell6state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>5</strong><br />';
$this->context->setstate($this->context->getcell5state());
}
public function goright()
{
echo '不合法的移动!<br />';
}
public function goup()
{
echo '走到<strong>3</strong><br />';
$this->context->setstate($this->context->getcell3state());
}
public function godown()
{
echo '走到<strong>9</strong><br />';
$this->context->setstate($this->context->getcell9state());
}
}
cell7state
<?php
class cell7state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '不合法的移动!<br />';
}
public function goright()
{
echo '走到<strong>8</strong><br />';
$this->context->setstate($this->context->getcell8state());
}
public function goup()
{
echo '走到<strong>4</strong><br />';
$this->context->setstate($this->context->getcell4state());
}
public function godown()
{
echo '不合法的移动!<br />';
}
}
cell8state
<?php
class cell8state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>7</strong><br />';
$this->context->setstate($this->context->getcell7state());
}
public function goright()
{
echo '走到<strong>9</strong><br />';
$this->context->setstate($this->context->getcell9state());
}
public function goup()
{
echo '走到<strong>5</strong><br />';
$this->context->setstate($this->context->getcell5state());
}
public function godown()
{
echo '不合法的移动!<br />';
}
}
cell9state
<?php
class cell9state implements imatrix
{
private $context;
public function __construct(context $contextnow)
{
$this->context = $contextnow;
}
public function goleft()
{
echo '走到<strong>8</strong><br />';
$this->context->setstate($this->context->getcell8state());
}
public function goright()
{
echo '不合法的移动!<br />';
}
public function goup()
{
echo '走到<strong>6</strong><br />';
$this->context->setstate($this->context->getcell6state());
}
public function godown()
{
echo '不合法的移动!<br />';
}
}
要想有效地使用状态设计模式, 真正的难点在于要想象现实或模拟世界是怎么样
客户client
下面从单元格5开始进行一个上,右,下, 下,左,上的移动
client.php
<?php
function __autoload($class_name)
{
include_once $class_name.'.php';
}
class client
{
private $context;
public function __construct()
{
$this->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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!