Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > (Design question) Formatting objects for file output

Reply
Thread Tools

(Design question) Formatting objects for file output

 
 
alacrite@gmail.com
Guest
Posts: n/a
 
      01-14-2007
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

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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".

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output file formatting/loop problems -- HELP? Maggie Python 5 09-08-2009 06:22 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Newbie: How do I filter output to the screen and writing the orginal output to a file? Mav Perl Misc 22 07-09-2004 09:56 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM



Advertisments