码迷,mamicode.com
首页 > Web开发 > 详细

php状态设计模式

时间:2018-09-29 00:07:42      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:this   核心   base   abstract   div   getc   medium   imp   一个   

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

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

标签:this   核心   base   abstract   div   getc   medium   imp   一个   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!