Caretaker:?
Short answer: The Caretaker class manages the mementos. It’s responsible for saving and undoing changes to the TextEditor. The Save() method pushes the current state (memento) onto a stack. The Undo() method pops the most recent memento off the stack and restores the TextEditor to that state. public class Caretaker
Example code
{
private readonly Stack<TextMemento> _mementos = new Stack<TextMemento>(); public void Save(TextEditor editor) => _mementos.Push(editor.Save()); public void Undo(TextEditor editor)
{
if (_mementos.Count > 0)
{ editor.Restore(_mementos.Pop()); }
}
}
Real-world example (ShopNest)
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png