0

Is memento pattern too over engineered

Refer code below of memento design pattern Is it not over engineered? Additionally, isn't it too much to do for just storing a variable? Is it not too much tightly coupled as variable name in one class has to be copied into other class. https://sololearn.com/compiler-playground/cOPP5KBjrevB/?ref=app

28th Apr 2025, 3:19 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Antworten
+ 2
it depends on what your use case is. if you want the save status to be triggered from somewhere else, having a separate class handling it, like your History class, is probably ok. Then you decide when status save occurs. But as your program runs, there would probably be misalignments between the saved state and actual object state between saves . It would not matter if you do a save when the program quits, but not for unexpected termination. if you want to save the state every time the object state changes, making the save state method a part of the object class is the more efficient option.
30th Apr 2025, 6:28 AM
Bob_Li
Bob_Li - avatar
0
I don't know CPP well enough to comment on the code itself, but the point of the Memento Pattern is that with a larger data set it will use a smaller amount of memory/CPU/storage than having to perform the evaluation on every copy of that same value. While the tests here may be smaller (IDK, haven't tried) there are larger tests on leetcode, etc, which may help better test your implementation. Larger meaning "unreasonable even if it is realistic".
30th Apr 2025, 4:16 AM
Tom Shaver
Tom Shaver - avatar
0
Thanks Tom Shaver but I am still confused about the memento saving memory. We need to save state and do save it through same class or additional memento class saves memory?
30th Apr 2025, 5:27 AM
Ketan Lalcheta
Ketan Lalcheta - avatar