mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 13:59:08 +02:00
- 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.
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace DesignPatterns\Behavioral\Memento;
|
|
|
|
class Caretaker
|
|
{
|
|
protected $history = array();
|
|
|
|
/**
|
|
* @return Memento
|
|
*/
|
|
public function getFromHistory($id)
|
|
{
|
|
return $this->history[$id];
|
|
}
|
|
|
|
/**
|
|
* @param Memento $state
|
|
*/
|
|
public function saveToHistory(Memento $state)
|
|
{
|
|
$this->history[] = $state;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
}
|