As we know the memento pattern is without violating encapsulation, capturing and externalize a object's internal state and can be reclaimed later without knowledge of the orginal state.
My question comes here how java.io.Serializable
is coming under this pattern because when ever we are serializing any private variable and writing the object state to a file at the same time the private varible's value is open to the world and the encapsulation seems to be failing here.
The memento pattern allows one to capture the internal state of an object without violating encapsulation such that later one can undo/revert the changes if required. Here one can see that the memento object is actually used to revert the changes made in the object.
Memento is a behavioral design pattern that allows making snapshots of an object's state and restoring it in future. The Memento doesn't compromise the internal structure of the object it works with, as well as data kept inside the snapshots.
Identify the roles of “caretaker” and “originator”. Create a Memento class and declare the originator a friend. Caretaker knows when to "check point" the originator. Originator creates a Memento and copies its state to that Memento.
Benefits of using memento design patternIt stores the objects state without compromising encapsulation. You can produce snapshots of the object's state without violating its encapsulation. You can simplify the originator's code by letting the caretaker maintain the history of the originator's state.
The Wikipedia article on the Memento pattern does not mention anything about encapsulation, in fact, the example given there captures exactly the state held in a private variable in a Memento.
Encapsulation ('A language mechanism for restricting access to some of the object's components') refers to how you have to write code in order change an object's internal state.
The internal state of an object could however have been determined by external input such as the content of a string depends on the file which it was read from or what data was received from the network. A checkbox' state depends on whether the user has checked it or not while the corresponding field in the class might have private access and the state might be read-only for other classes.
Protecting fields by putting them under private access is meant to help the developer keep the states of objects in a consistent state, i.e. avoid that fields are set to an inconsistent state from code outside that class (for example if the value of field A
depends on the value of field B
).
It has nothing to do with 'privacy' in the sense that this data is considered to be secret. Of course, one could write another class which then reads the serialized private fields and makes them publicly available in a different class or you could even edit the serialized file but I'm not sure what one would gain from this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With