responsibility for storage

Posted by Stefano Borini on Programmers See other posts from Programmers or by Stefano Borini
Published on 2011-02-24T09:32:15Z Indexed on 2011/02/24 15:32 UTC
Read the original article Hit count: 333

Filed under:
|
|

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:

  1. object.store(file)
  2. 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.

© Programmers or respective owner

Related posts about design

Related posts about io