Евгений Глотов 0ebafc5eaa Memento pattern.
- Caretaker should save history for normal pattern implementation.
- Importent notes in code of Originator.
- Readme with full pattern philosophy.
- Tests and example of usage inside test.
2015-05-30 05:35:54 +03:00

39 lines
856 B
PHP

<?php
namespace DesignPatterns\Behavioral\Memento;
class Originator
{
/* @var mixed */
private $state;
// The class could also contain additional data that is not part of the
// state saved in the memento..
/**
* @param mixed $state
*/
public function setState($state)
{
// you must check type of state inside child of this class
// or use type-hinting for full pattern implementation
$this->state = $state;
}
/**
* @return Memento
*/
public function getStateAsMemento()
{
// you must save a separate copy in Memento
$state = is_object($this->state) ? clone $this->state : $this->state;
return new Memento($state);
}
public function restoreFromMemento(Memento $memento)
{
$this->state = $memento->getState();
}
}