🎹 A use case for empty interfaces:

Creating an empty interface "I" allows us to make explicit that objects of type "I" must be opaque. You can reference objects of type "I" and pass them around, but you can't invoke behavior.

Example: the Memento design pattern.

đŸ§” 1/5
The intent of the Memento pattern is to support the creation of snapshops of the internal state of objects so that these objects can be restored to this state later.

That is, the Memento pattern helps us implement "undo" operations.

This is the structure of the pattern: 2/5
1⃣ The Caretaker (the "undo mechanism") will request a Memento from the Originator by calling "createMemento".

A Memento is an object that stores a snapshot of the Originator's internal state.

2⃣ The Caretaker will hold the Memento for a while. 3/5
3⃣ When an "undo" operation is requested, the Caretaker will hand the Memento back to the Originator (by calling "setMemento") so that it can reconstitute itself to its previous state (the state stored in the Memento).

🔑 Here is the key thing: 4/5
The Memento is a private record of the internal state of the Originator. To preserve encapsulation, the Caretaker sees an opaque interface to the Memento. It cannot operate on or examine the contents of the Memento.

This is achieved thanks to the "Memento" empty interface.
5/5
You can follow @macerub.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.