Exforsys

Online Training

Generic way to load resources

This is a discussion on Generic way to load resources within the Software Patterns forums, part of the Testing category; Hi, I am currently developping a program that uses files to store data. There are different sorts of data: preferences ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-14-2005, 12:38 PM
Knolselder
 
Posts: n/a
Generic way to load resources

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.

Greetz,

Jan Meskens
Student Computer Science

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-14-2005, 02:19 PM
Oliver Wong
 
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-11-2005, 12:39 PM
iscy
 
Posts: n/a
Re: Generic way to load resources

The use of the factory here, is a good decision. I can see that your
current problem is to make the file evolve with time. What you'll need
for this is a better file format. Take examples from XML. XML is very
good for a file that can change with time and still be supported. In
other words, it's not hard to create a XML version of a file that work
with both older and newer version of a software.

By seeing this, you'll probably say: yeah, but I don't want to use XML
to save things up. People could modify it or .....

Yes I agree. Don't take XML! But take the concept. Just try to
understand what is so special with XML...

iscy

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-12-2005, 09:58 AM
Oliver Wong
 
Posts: n/a
Re: Generic way to load resources


"iscy" <iscy@claurendeau.no-ip.com> wrote in message
news:1134322742.703378.68030@g14g2000cwa.googlegroups.com...
> The use of the factory here, is a good decision. I can see that your
> current problem is to make the file evolve with time. What you'll need
> for this is a better file format. Take examples from XML. XML is very
> good for a file that can change with time and still be supported. In
> other words, it's not hard to create a XML version of a file that work
> with both older and newer version of a software.
>
> By seeing this, you'll probably say: yeah, but I don't want to use XML
> to save things up. People could modify it or .....
>
> Yes I agree. Don't take XML! But take the concept. Just try to
> understand what is so special with XML...


The secret behind XML's extensibility is the behaviour of "Ignore tags
you don't understand".

- Oliver


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-02-2006, 01:35 AM
iscy
 
Posts: n/a
Re: Generic way to load resources

This is not really what I tried to mean...

iscy

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new questions
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:51 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.