View Single Post

  #2 (permalink)  
Old 11-14-2005, 03:19 PM
Oliver Wong
Guest
 
Posts: n/a
Re: Generic way to load resources


"Knolselder" <knolselder2003@hotmail.com> wrote in message
newsan.2005.11.14.19.38.22.364759@hotmail.com...
> Hi,
>
> I am currently developping a program that uses files to store data. There
> are different sorts of data: preferences about the program, high scores
> (it 's a game) and document data (the main data of the program - a
> game-map in this case).
>
> My problem is the following: how do I create an object-structure to load
> data from files in a flexible way? With flexible I mean:
> - When I change the file-format, I need to hack in as less classes as
> possible.
> - When I migrate from files to a database in the future, it must be easy
> in my
> design.
> - When I add extra data in a later version of the program, or support
> different
> file formats for the data, it must also be possible.
>
> I tought that the 'abstract factory pattern' could be a solution for my
> problem. I make an 'abstractdatafactory' object and for each data-format a
> 'concretefactory'. Each 'concrete factory' will implement the abstract
> method 'loadData(file : String) : Data' of the 'abstract factory'. Is this
> a good way to load data? And is it also permitted to put the 'save part'
> in the factory classes?
>
> Has sombody proposals to solve this problem? Any kind of tips or tricks
> are appreciated.


No particular (set of) design patterns immediately jump to mind. I think
you just need to have a good division of responsibilities.

Have one class (or set of classes) that takes care of getting a stream
of bytes. For example, this class might simply be given a string that
uniquely identifies the resource, and then gets the stream of bytes that
make up the resource. Then, when you switch from files to DB, only this
class needs to change.

Have another class (or set of classes) that takes care of building
instances of objects based on byte streams that they are provided. That way,
when the file format changes (or new data is added), this is the only class
that changes.

For saving, reverse the process: Use the latter classes to convert a
live object into a byte stream, then use the former classes to store that
bytestream either to the disk or the DB as appropriate.

- Oliver


Reply With Quote