mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 11:40:13 +02:00
Implemented Memento Pattern
This commit is contained in:
33
Memento/Caretaker.php
Normal file
33
Memento/Caretaker.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Memento;
|
||||
|
||||
class Caretaker
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
25
Memento/Memento.php
Normal file
25
Memento/Memento.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Memento;
|
||||
|
||||
class Memento
|
||||
{
|
||||
/* @var string */
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @param string $stateToSave
|
||||
*/
|
||||
public function __construct($stateToSave)
|
||||
{
|
||||
$this->state = $stateToSave;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
}
|
33
Memento/Originator.php
Normal file
33
Memento/Originator.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Memento;
|
||||
|
||||
class Originator
|
||||
{
|
||||
/* @var string */
|
||||
private $state;
|
||||
|
||||
// The class could also contain additional data that is not part of the
|
||||
// state saved in the memento..
|
||||
|
||||
/**
|
||||
* @param string $state
|
||||
*/
|
||||
public function setState($state)
|
||||
{
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Memento
|
||||
*/
|
||||
public function saveToMemento()
|
||||
{
|
||||
return new Memento($this->state);
|
||||
}
|
||||
|
||||
public function restoreFromMemento(Memento $memento)
|
||||
{
|
||||
$this->state = $memento->getState();
|
||||
}
|
||||
}
|
18
Memento/README.md
Normal file
18
Memento/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Memento
|
||||
|
||||
## Purpose
|
||||
|
||||
Provide the ability to restore an object to its previous state (undo via rollback).
|
||||
|
||||
The memento pattern is implemented with three objects: the originator, a caretaker and a memento.
|
||||
The originator is some object that has an internal state.
|
||||
The caretaker is going to do something to the originator, but wants to be able to undo the change.
|
||||
The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do.
|
||||
To roll back to the state before the operations, it returns the memento object to the originator.
|
||||
The memento object itself is an opaque object (one which the caretaker cannot, or should not, change).
|
||||
When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
|
||||
|
||||
## Examples
|
||||
|
||||
* The seed of a pseudorandom number generator
|
||||
* The state in a finite state machine
|
@@ -40,6 +40,7 @@ The patterns can be structured in roughly three different categories. Please cli
|
||||
* [Command](Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern)
|
||||
* [Iterator](Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern)
|
||||
* [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern)
|
||||
* [Memento](Memento) [:notebook:](http://http://en.wikipedia.org/wiki/Memento_pattern)
|
||||
* [NullObject](NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern)
|
||||
* [Observer](Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern)
|
||||
* [Specification](Specification) [:notebook:](http://en.wikipedia.org/wiki/Specification_pattern)
|
||||
|
36
Tests/Memento/MementoTest.php
Normal file
36
Tests/Memento/MementoTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\Memento;
|
||||
|
||||
use DesignPatterns\Memento;
|
||||
|
||||
/**
|
||||
* MementoTest tests memento design pattern
|
||||
*/
|
||||
class MementoTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testOriginator()
|
||||
{
|
||||
$originator = new Memento\Originator();
|
||||
$originator->setState("State1");
|
||||
|
||||
$this->assertAttributeEquals("State1", "state", $originator);
|
||||
|
||||
$originator->setState("State2");
|
||||
|
||||
$this->assertAttributeEquals("State2", "state", $originator);
|
||||
|
||||
$savedState = $originator->saveToMemento();
|
||||
|
||||
$this->assertAttributeEquals("State2", "state", $savedState);
|
||||
|
||||
$originator->setState("State3");
|
||||
|
||||
$this->assertAttributeEquals("State3", "state", $originator);
|
||||
|
||||
$originator->restoreFromMemento($savedState);
|
||||
|
||||
$this->assertAttributeEquals("State2", "state", $originator);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user