By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

(Design question) Formatting objects for file output

P: n/a
I have a class that represents a record in a database table.

class tableName
{
int col1;
string col2;
int col3;
... other fields and relevant operations
}

I want to output that object's contents into a file. The user has
different options for the formatting used(csv,xml,pipe, ect.).

The design I am using right now is use an base class to define the
interface.

class dbWriter
{
string write(const tableName &);
}

then create subclasses for each format

class dbWriterToXml : public dbWriter
{
//this write will take a tableName obj and return a xml string
representation
string write(const tableName &);
}
class dbWriterToCsv : public dbWriter
{
//this write will take a tableName obj and return a csv string
representation
string write(const tableName &);
}

The reason for the subclasses is so that when these classes are to be
written to a file you could do something lik e this

you could have a set format function in the class that is actually
driving this process
iWantToWriteARecodToAFile.setFormat("CSV");
which sets the string outputFormat variable

then when the class that wants to actually do the writing calls its
function to writeToFile
iWantToWriteARecodToAFile.writeToFile();

the contents of the method writeToFile() would look something like this
dbWriter* db_writer = getWriterClassFromFactory(outputFormat);
//factory returns proper format class
outputMessage = db_writer.write(); //polymorphically call the correct
write function
....//write the outputMessage to the file

Questions is that good OO design?

one alternative I can think of:
class dbWriter
{
writeAsXML();
writeAsCSV();
writeAsPipe();
}

then just use if statements
if(outputFormat == "CSV")
{
db_writer.writeAsXML();
}
else if ...

the difference that I see between these two approaches is that as new
formats are created one requires changes to the factory the other to
the if else statements.

Which approach is better and why? Any alternatives that would be better
than each of these approaches?

thanks
-Jake

Jan 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
al******@gmail.com wrote:
>
I have a class that represents a record in a database table.

I want to output that object's contents into a file. The user has
different options for the formatting used(csv,xml,pipe, ect.).

The design I am using right now is use an base class to define the
interface.
Try use design pattern "builder" in which each of "(csv,xml,pipe,
ect.)" will be as "concrete builder",
and parser of your table will be as "director".

Jan 15 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.