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.
encapsulation
. – Boone