它提供了将对象恢复到之前状态(通过回滚撤销)或访问对象状态的能力,而不需要揭示它的实现(对象不需要具有返回当前状态的函数)。
memento模式由三个对象实现:Originator, Caretaker, Memento。
Memento – 包含任何对象或资源状态的具体唯一快照的对象:字符串、数字、数组、类的实例等。 这种情况下的唯一性并不意味着禁止在不同的快照中存在相似的状态。 这意味着可以将状态提取为独立克隆。 存储在 Memento 中的任何对象都应该是原始对象的完整副本,而不是对原始对象的引用。 Memento 对象是一个“不透明对象”(没有人可以或不应该更改的对象)。
Originator—它是一个包含外部对象实际状态的对象,是严格指定的类型。Originator能够创建此状态的唯一副本,并将其包裹在Memento中返回。 Originator不知道变化的历史。 您可以从外部将具体状态设置为Originator,这将被视为实际状态。 Originator必须确保给定的状态对应于允许的对象类型。Originator可能(但不应该)有任何方法,但他们不能对保存的对象状态进行更改。
Caretaker控制着状态的历史。 他可以对一个对象进行更改; 决定在Originator中保存外部对象的状态;从当前状态的Originator快照中询问; 或将Originator状态设置为与历史记录中的某些快照等效。
Memento.php
<?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Memento;
class Memento
{
public function __construct(private State $state)
{
}
public function getState(): State
{
return $this->state;
}
}
State.php
<?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Memento;
use InvalidArgumentException;
class State implements \Stringable
{
public const STATE_CREATED = 'created';
public const STATE_OPENED = 'opened';
public const STATE_ASSIGNED = 'assigned';
public const STATE_CLOSED = 'closed';
private string $state;
/**
* @var string[]
*/
private static array $validStates = [
self::STATE_CREATED,
self::STATE_OPENED,
self::STATE_ASSIGNED,
self::STATE_CLOSED,
];
public function __construct(string $state)
{
self::ensureIsValidState($state);
$this->state = $state;
}
private static function ensureIsValidState(string $state)
{
if (!in_array($state, self::$validStates)) {
throw new InvalidArgumentException('Invalid state given');
}
}
public function __toString(): string
{
return $this->state;
}
}
Ticket.php
<?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Memento;
/**
* Ticket is the "Originator" in this implementation
*/
class Ticket
{
private State $currentState;
public function __construct()
{
$this->currentState = new State(State::STATE_CREATED);
}
public function open()
{
$this->currentState = new State(State::STATE_OPENED);
}
public function assign()
{
$this->currentState = new State(State::STATE_ASSIGNED);
}
public function close()
{
$this->currentState = new State(State::STATE_CLOSED);
}
public function saveToMemento(): Memento
{
return new Memento(clone $this->currentState);
}
public function restoreFromMemento(Memento $memento)
{
$this->currentState = $memento->getState();
}
public function getState(): State
{
return $this->currentState;
}
}
Tests/MementoTest.php
<?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Memento\Tests;
use DesignPatterns\Behavioral\Memento\State;
use DesignPatterns\Behavioral\Memento\Ticket;
use PHPUnit\Framework\TestCase;
class MementoTest extends TestCase
{
public function testOpenTicketAssignAndSetBackToOpen()
{
$ticket = new Ticket();
// open the ticket
$ticket->open();
$openedState = $ticket->getState();
$this->assertSame(State::STATE_OPENED, (string) $ticket->getState());
$memento = $ticket->saveToMemento();
// assign the ticket
$ticket->assign();
$this->assertSame(State::STATE_ASSIGNED, (string) $ticket->getState());
// now restore to the opened state, but verify that the state object has been cloned for the memento
$ticket->restoreFromMemento($memento);
$this->assertSame(State::STATE_OPENED, (string) $ticket->getState());
$this->assertNotSame($openedState, $ticket->getState());
}
}