美文网首页
状态模式

状态模式

作者: 散装咖啡 | 来源:发表于2017-05-30 03:10 被阅读6次
    /**
    * state模式
    *
    * 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它
    所属的类
    *
    */
    
        interface State
        {
            public function handle($state);
            public function display();
        }
    
        class Context
        {
            private $_state = null;
            
            public function __construct($state)
            {
                $this->setState($state);
            }
            
            public function setState($state)
            {
                $this->_state =$state;
            }
            
            public function request()
            {
                $this->_state->display();
                $this->_state->handle($this);
            }
        }
    
        class StateA implements State
        {
            public function handle($context)
            {
                $context->setState(new StateB());
            }
            
            public function display()
            {
                echo "state A<br/>";
            }
        }
    
        class StateB implements State
        {
            public function handle($context)
            {
                $context->setState(new StateC());
            }
            
            public function display()
            {
                echo"state B<br/>";
            }
        }
    
        class StateC implements State
        {
            public function handle($context)
            {
                $context->setState(new StateA());
            }
            
            public function display()
            {
                echo "state C<br/>";
            }
        }
        
    // 实例化一下
    $objContext = new Context(new StateB());
    $objContext->request();//B
    $objContext->request();//C
    $objContext->request();//A
    $objContext->request();//B
    $objContext->request();//C
    

    参考文章 https://wenku.baidu.com/view/b4c78b4702768e9951e738fb.html

    相关文章

      网友评论

          本文标题:状态模式

          本文链接:https://www.haomeiwen.com/subject/dsglfxtx.html