From acc
Generates Memento design pattern in PHP 8.4 for undo/redo state capture and restoration, with originator, memento, caretaker, value objects, and unit tests.
How this skill is triggered — by the user, by Claude, or both
Slash command
/acc:create-mementoThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Creates Memento pattern infrastructure for capturing and restoring object state without violating encapsulation.
Creates Memento pattern infrastructure for capturing and restoring object state without violating encapsulation.
| Scenario | Example |
|---|---|
| Undo/Redo functionality | Document editor, form drafts, game state |
| State snapshots | Transaction rollback, checkpoint systems |
| State history tracking | Audit trail, version history |
| Transactional operations | Multi-step processes with rollback |
Path: src/Domain/{BoundedContext}/Memento/
{Name}Memento.php — Immutable state snapshotPath: src/Domain/{BoundedContext}/
{Name}.php — Object with createMemento() and restore() methodsPath: src/Application/{BoundedContext}/
{Name}History.php — Manages memento stack for undo/redoPath: src/Domain/{BoundedContext}/ValueObject/
{State}.php — State representation value object{Name}MementoTest.php — Memento creation tests{Name}HistoryTest.php — Caretaker undo/redo tests| Component | Path |
|---|---|
| Memento | src/Domain/{BoundedContext}/Memento/ |
| Originator | src/Domain/{BoundedContext}/ |
| Caretaker | src/Application/{BoundedContext}/ |
| Value Objects | src/Domain/{BoundedContext}/ValueObject/ |
| Unit Tests | tests/Unit/Domain/{BoundedContext}/ |
| Component | Pattern | Example |
|---|---|---|
| Memento | {Name}Memento | DocumentMemento |
| Originator | {Name} | Document |
| Caretaker | {Name}History | DocumentHistory |
| Create Method | createMemento() | createMemento() |
| Restore Method | restore() or restoreFromMemento() | restore() |
| Test | {ClassName}Test | DocumentMementoTest |
final readonly class {Name}Memento
{
public function __construct(
private {StateType} $state,
private \DateTimeImmutable $createdAt
) {}
public function state(): {StateType}
{
return $this->state;
}
public function createdAt(): \DateTimeImmutable
{
return $this->createdAt;
}
}
final class {Name}
{
private {StateType} $state;
public function createMemento(): {Name}Memento
{
return new {Name}Memento(
state: $this->state,
createdAt: new \DateTimeImmutable()
);
}
public function restore({Name}Memento $memento): void
{
$this->state = $memento->state();
}
}
final class {Name}History
{
private array $mementos = [];
private int $currentIndex = -1;
public function save({Name}Memento $memento): void
{
$this->mementos = array_slice($this->mementos, 0, $this->currentIndex + 1);
$this->mementos[] = $memento;
++$this->currentIndex;
}
public function undo(): ?{Name}Memento
{
if ($this->currentIndex > 0) {
--$this->currentIndex;
return $this->mementos[$this->currentIndex];
}
return null;
}
public function redo(): ?{Name}Memento
{
if ($this->currentIndex < count($this->mementos) - 1) {
++$this->currentIndex;
return $this->mementos[$this->currentIndex];
}
return null;
}
}
// Create originator
$document = new Document(content: 'Initial text');
// Create caretaker
$history = new DocumentHistory();
// Save initial state
$history->save($document->createMemento());
// Make changes
$document->setContent('Modified text');
$history->save($document->createMemento());
$document->setContent('Final text');
$history->save($document->createMemento());
// Undo changes
$memento = $history->undo();
if ($memento) {
$document->restore($memento); // Back to 'Modified text'
}
// Redo changes
$memento = $history->redo();
if ($memento) {
$document->restore($memento); // Forward to 'Final text'
}
| Domain | Use Cases |
|---|---|
| Document Editor | Text content, formatting, cursor position |
| Form Management | Draft state, field values, validation state |
| Game Development | Player state, level progress, inventory |
| Order Processing | Order draft, item changes, pricing snapshots |
| Configuration | Settings snapshots, rollback points |
| Anti-pattern | Problem | Solution |
|---|---|---|
| Mutable memento | State corruption | Use readonly classes |
| Public state access | Breaks encapsulation | Expose only via getters |
| Large state copies | Memory overhead | Store only changed fields |
| Missing timestamp | No audit trail | Include createdAt |
| Unbounded history | Memory leak | Implement history limit |
For complete PHP templates and examples, see:
references/templates.md — Memento, Originator, Caretaker templatesreferences/examples.md — Document, Order, Form state management with testsnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accCaptures and restores object state for undo/redo, rollback, or snapshot functionality without breaking encapsulation.
Implements GoF Memento pattern in TypeScript for undo/redo functionality and state snapshots, preserving encapsulation. Useful for editors, games, or any system requiring time-travel state restoration.
Generates State pattern for PHP 8.4 including context, state interface, concrete states, factory, entity updates, exceptions, and unit tests for state-dependent object behavior like order workflows.