Skip to content

Restricted _state - Memento Design Pattern in Python #8

Open
@JuanPabloArbelaez

Description

@JuanPabloArbelaez

Hello.

I am learning design patterns through the implementations in Python.

The Memento design pattern is supposed to restore previous states, while keeping the information about them, restricted to other objects.

However when implementing the code, I was able to get access to all the restricted states:

if __name__ == "__main__":
    originator = Originator("Super-duper-super-puper-super.")
    caretaker = Caretaker(originator)

    caretaker.backup()
    originator.do_something()
    
    ### this should not work in theory
    for m in caretaker._mementos:
          print(f"_state = {m.get_state()}")  

as seen in the for loop:

I can print all the values of the previous states.

_state = Super-duper-super-puper-super.
_state = kTjoIhPwiBmqvJfuDENWZXKcdUMHtp

is there a way to actually hide these states in Python?

Thanks for your work teaching design patterns. I hope you can guide me on this subject.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions