Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 314fc18

Browse files
committed
Implemented Memento Pattern
1 parent ed291cd commit 314fc18

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

Memento/Caretaker.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace DesignPatterns\Memento;
4+
5+
class Caretaker
6+
{
7+
public static function run()
8+
{
9+
/* @var $savedStates Memento[] */
10+
11+
$savedStates = array();
12+
13+
$originator = new Originator();
14+
15+
//Setting state to State1
16+
$originator->setState("State1");
17+
//Setting state to State2
18+
$originator->setState("State2");
19+
//Saving State2 to Memento
20+
$savedStates[] = $originator->saveToMemento();
21+
//Setting state to State3
22+
$originator->setState("State3");
23+
24+
// We can request multiple mementos, and choose which one to roll back to.
25+
// Saving State3 to Memento
26+
$savedStates[] = $originator->saveToMemento();
27+
//Setting state to State4
28+
$originator->setState("State4");
29+
30+
$originator->restoreFromMemento($savedStates[1]);
31+
//State after restoring from Memento: State3
32+
}
33+
}

Memento/Memento.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DesignPatterns\Memento;
4+
5+
class Memento
6+
{
7+
/* @var string */
8+
private $state;
9+
10+
/**
11+
* @param string $stateToSave
12+
*/
13+
public function __construct($stateToSave)
14+
{
15+
$this->state = $stateToSave;
16+
}
17+
18+
/**
19+
* @return string
20+
*/
21+
public function getState()
22+
{
23+
return $this->state;
24+
}
25+
}

Memento/Originator.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace DesignPatterns\Memento;
4+
5+
class Originator
6+
{
7+
/* @var string */
8+
private $state;
9+
10+
// The class could also contain additional data that is not part of the
11+
// state saved in the memento..
12+
13+
/**
14+
* @param string $state
15+
*/
16+
public function setState($state)
17+
{
18+
$this->state = $state;
19+
}
20+
21+
/**
22+
* @return Memento
23+
*/
24+
public function saveToMemento()
25+
{
26+
return new Memento($this->state);
27+
}
28+
29+
public function restoreFromMemento(Memento $memento)
30+
{
31+
$this->state = $memento->getState();
32+
}
33+
}

Memento/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Memento
2+
3+
## Purpose
4+
5+
Provide the ability to restore an object to its previous state (undo via rollback).
6+
7+
The memento pattern is implemented with three objects: the originator, a caretaker and a memento.
8+
The originator is some object that has an internal state.
9+
The caretaker is going to do something to the originator, but wants to be able to undo the change.
10+
The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do.
11+
To roll back to the state before the operations, it returns the memento object to the originator.
12+
The memento object itself is an opaque object (one which the caretaker cannot, or should not, change).
13+
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.
14+
15+
## Examples
16+
17+
* The seed of a pseudorandom number generator
18+
* The state in a finite state machine

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The patterns can be structured in roughly three different categories. Please cli
4040
* [Command](Command) [:notebook:](http://en.wikipedia.org/wiki/Command_pattern)
4141
* [Iterator](Iterator) [:notebook:](http://en.wikipedia.org/wiki/Iterator_pattern)
4242
* [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern)
43+
* [Memento](Memento) [:notebook:](http://http://en.wikipedia.org/wiki/Memento_pattern)
4344
* [NullObject](NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern)
4445
* [Observer](Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern)
4546
* [Specification](Specification) [:notebook:](http://en.wikipedia.org/wiki/Specification_pattern)

Tests/Memento/MementoTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Memento;
4+
5+
use DesignPatterns\Memento;
6+
7+
/**
8+
* MementoTest tests memento design pattern
9+
*/
10+
class MementoTest extends \PHPUnit_Framework_TestCase
11+
{
12+
13+
public function testOriginator()
14+
{
15+
$originator = new Memento\Originator();
16+
$originator->setState("State1");
17+
18+
$this->assertAttributeEquals("State1", "state", $originator);
19+
20+
$originator->setState("State2");
21+
22+
$this->assertAttributeEquals("State2", "state", $originator);
23+
24+
$savedState = $originator->saveToMemento();
25+
26+
$this->assertAttributeEquals("State2", "state", $savedState);
27+
28+
$originator->setState("State3");
29+
30+
$this->assertAttributeEquals("State3", "state", $originator);
31+
32+
$originator->restoreFromMemento($savedState);
33+
34+
$this->assertAttributeEquals("State2", "state", $originator);
35+
}
36+
}

0 commit comments

Comments
 (0)