responsibility for storage
- by Stefano Borini
A colleague and I were brainstorming about where to put the responsibility of an object to store itself on the disk in our own file format. There are basically two choices:
object.store(file)
fileformatWriter.store(object)
The first one gives the responsibility of serialization on the disk to the object itself. This is similar to the approach used by python pickle.
The second groups the representation responsibility on a file format writer object. The data object is just a plain data container (eventually with additional methods not relevant for storage).
We agreed on the second methodology, because it centralizes the writing logic from generic data. We also have cases of objects implementing complex logic that need to store info while the logic is in progress. For these cases, the fileformatwriter object can be passed and used as a delegate, calling storage operations on it. With the first pattern, the complex logic object would instead accept the raw file, and implement the writing logic itself.
The first method, however, has the advantage that the object knows how to write and read itself from any file containing it, which may also be convenient.
I would like to hear your opinion before starting a rather complex refactoring.