Dclass Memento:
def save(self, state):
self.state = state
Step-by-Step Solution
Solution:
Step 1: Understand Memento class structure
The Memento class should have an initializer that accepts the state and stores it privately.
Step 2: Check options for correct syntax and encapsulation
class Memento:
def __init__(self, state):
self._state = state correctly defines __init__ with a private variable _state. Others either use wrong method names or do not initialize state properly.
Final Answer:
class Memento:\n def __init__(self, state):\n self._state = state -> Option B
Quick Check:
Correct Memento init = class Memento:
def __init__(self, state):
self._state = state [OK]
Quick Trick:Use __init__ to set private state in Memento [OK]
Common Mistakes:
MISTAKES
Using wrong method names like __save__
Not initializing state in constructor
Making state public without underscore
Master "Behavioral Design Patterns — Part 2" in LLD
9 interactive learning modes - each teaches the same concept differently