コード例 #1
0
ファイル: front.php プロジェクト: ZerGabriel/cms-1
 public function before()
 {
     parent::before();
     $this->_ctx =& Context::instance();
     $this->_ctx->request($this->request)->response($this->response);
     View::bind_global('ctx', $this->_ctx);
 }
コード例 #2
0
ファイル: client.php プロジェクト: luisOO/design-patterns
 /**
  * Main program
  */
 public static function main()
 {
     $context = new Context();
     $context->request();
     $context->request();
     $context->request();
     $context = null;
 }
コード例 #3
0
ファイル: State.php プロジェクト: edin/php-design-patterns
            $c->state = new NormalState();
            echo "<Normal state>\n";
        }
        $c->counter -= 5;
        return $c->counter;
    }
}
class Context
{
    const LIMIT = 10;
    public $state;
    public $counter;
    public function __construct()
    {
        $this->counter = self::LIMIT;
    }
    public function request(int $i) : int
    {
        if ($i == 2) {
            return $this->state->moveUp($this);
        } else {
            return $this->state->moveDown($this);
        }
    }
}
$context = new Context();
$context->state = new NormalState();
for ($i = 0; $i < 500; ++$i) {
    $r = mt_rand(1, 3);
    echo $context->request($r), "\n";
}
コード例 #4
0
ファイル: State.php プロジェクト: rick00young/StudyNote
    }
}
class StateB implements State
{
    public function handle($context)
    {
        $context->setState(new StateC());
    }
    public function display()
    {
        echo "State B\n";
    }
}
class StateC implements State
{
    public function handle($context)
    {
        $context->setState(new StateA());
    }
    public function display()
    {
        echo "State C\n";
    }
}
//实例化
$objContext = new Context(new StateB());
$objContext->request();
$objContext->request();
$objContext->request();
$objContext->request();
$objContext->request();
コード例 #5
0
    }
    public function handle(Context $context)
    {
        echo 'concrete_b' . "<br>";
        $context->setState(ConcreteStateA::getInstance());
    }
}
class Context
{
    // 环境角色
    private $_state;
    public function __construct()
    {
        // 默认为stateA
        $this->_state = ConcreteStateA::getInstance();
    }
    public function setState(State $state)
    {
        $this->_state = $state;
    }
    public function request()
    {
        $this->_state->handle($this);
    }
}
// client
$context = new Context();
$context->request();
$context->request();
$context->request();
$context->request();