setState() public method

public setState ( $state )
Beispiel #1
0
 public function testMemento()
 {
     $originator = new Originator();
     $originator->setState("On");
     $this->assertEquals("On", $originator->getState());
     $memento = $originator->createMemento();
     $this->assertInstanceOf('Behavioral\\Memento\\Memento', $memento);
     $caretaker = new Caretaker();
     $caretaker->setMemento($memento);
     $originator->setState('Off');
     $this->assertEquals("Off", $originator->getState());
     $memento = $caretaker->getMemento();
     $this->assertInstanceOf('Behavioral\\Memento\\Memento', $memento);
     $originator->restore($memento);
     $this->assertEquals("On", $originator->getState());
 }
 public function runCustomLogic()
 {
     $originator = new Originator();
     //Setting state to State1
     $originator->setState('State1');
     //Setting state to State2
     $originator->setState('State2');
     //Saving State2 to Memento
     $this->saveToHistory($originator->getStateAsMemento());
     //Setting state to State3
     $originator->setState('State3');
     // We can request multiple mementos, and choose which one to roll back to.
     // Saving State3 to Memento
     $this->saveToHistory($originator->getStateAsMemento());
     //Setting state to State4
     $originator->setState('State4');
     $originator->restoreFromMemento($this->getFromHistory(1));
     //State after restoring from Memento: State3
     return $originator->getStateAsMemento()->getState();
 }
 public static function run()
 {
     /* @var $savedStates Memento[] */
     $savedStates = array();
     $originator = new Originator();
     //Setting state to State1
     $originator->setState("State1");
     //Setting state to State2
     $originator->setState("State2");
     //Saving State2 to Memento
     $savedStates[] = $originator->saveToMemento();
     //Setting state to State3
     $originator->setState("State3");
     // We can request multiple mementos, and choose which one to roll back to.
     // Saving State3 to Memento
     $savedStates[] = $originator->saveToMemento();
     //Setting state to State4
     $originator->setState("State4");
     $originator->restoreFromMemento($savedStates[1]);
     //State after restoring from Memento: State3
 }
    private $_state = null;
    public function getState()
    {
        return $this->_state;
    }
    public function setState($state)
    {
        $this->_state = $state;
    }
    public function createMemento()
    {
        return new Memento($this->_state);
    }
    public function setMemento($memento)
    {
        $this->_state = $memento->getState();
    }
    public function display()
    {
        echo "state = " . $this->_state . "<br/>";
    }
}
$objOriginator = new Originator();
$objOriginator->setState(0);
$objOriginator->display();
$objCareTaker = new CareTaker();
$objCareTaker->setMemento($objOriginator->createMemento());
$objOriginator->setState(1);
$objOriginator->display();
$objOriginator->setMemento($objCareTaker->getMemento());
$objOriginator->display();
{
    // 负责人(Caretaker)角色
    private $_memento;
    public function getMemento()
    {
        return $this->_memento;
    }
    public function setMemento(Memento $memento)
    {
        $this->_memento = $memento;
    }
}
// client
/* 创建目标对象 */
$org = new Originator();
$org->setState('open');
$org->showState();
/* 创建备忘 */
$memento = $org->createMemento();
/* 通过Caretaker保存此备忘 */
$caretaker = new Caretaker();
$caretaker->setMemento($memento);
/* 改变目标对象的状态 */
$org->setState('close');
$org->showState();
$org->restoreMemento($memento);
$org->showState();
/* 改变目标对象的状态 */
$org->setState('close');
$org->showState();
/* 还原操作 */
Beispiel #6
0
 public static function main()
 {
     //创建目标对象
     $orig = new Originator();
     $orig->setState('open');
     $orig->showState();
     //创建备忘
     $memento = $orig->createMemento();
     //通过Caretaker保存此备忘
     $caretaker = new Caretaker();
     $caretaker->setMemento($memento);
     //改变目标对象的状态
     $orig->setState('close');
     $orig->showState();
     //还原操作
     $orig->restoreMemento($caretaker->getMemento());
     $orig->showState();
 }
Beispiel #7
0
 public function __construct()
 {
     $this->first = 'first';
     $this->second = 'second';
     $this->ct = new Caretaker();
     $this->orig = new Originator();
     $this->orig->setState($this->first);
     $this->ct->addMemento($this->orig->createMemento());
     $this->orig->setState($this->second);
     $this->ct->addMemento($this->orig->createMemento());
     $this->orig->setMemento($this->ct->getMemento(1));
     echo $this->orig->state;
 }
Beispiel #8
0
/*
 * 管理员
 */
class caretaker
{
    private $_mememto;
    public function setMemento($memento)
    {
        $this->_mememto = $memento;
    }
    public function getMemento()
    {
        return $this->_mememto;
    }
}
//客户端代码
//备份前的状态
$originator = new Originator();
$originator->setState('on');
$originator->showState();
//创建备忘录
$memento = $originator->createMemento();
//通过管理员保存此状态
$caretaker = new caretaker();
$caretaker->setMemento($memento);
//改变目前对象的状态
$originator->setState('off');
$originator->showState();
//还原操作
$originator->restoreMemento($caretaker->getMemento());
$originator->showState();