473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

(Design question) Formatting objects for file output

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
1 1502
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: grz02 | last post by:
Hi, Im an experienced database+software designer and developer, but, unfortunately, anything to do with web-programming and web-systems designs is still a pretty new area to me... (been working...
4
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
9
by: chris | last post by:
I've been scripting with python for a while now. Basically writing a few functions and running in the ipython shell. That's been very useful. But the more I do this the more I see that I'm doing...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
0
by: _ | last post by:
Hi Pythonistas and Pythonistos, I am doing some fairly complex statistical analyses and trying to display the output in a form readable to technicians not yet enlightened by the wonders of...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.