473,769 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(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,pi pe, 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
iWantToWriteARe codToAFile.setF ormat("CSV");
which sets the string outputFormat variable

then when the class that wants to actually do the writing calls its
function to writeToFile
iWantToWriteARe codToAFile.writ eToFile();

the contents of the method writeToFile() would look something like this
dbWriter* db_writer = getWriterClassF romFactory(outp utFormat);
//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.write AsXML();
}
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 1515
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,pi pe, 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,p ipe,
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
4867
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 mostly with "legacy" environments the last 10 years) So I am writing this, hoping to get some useful advise and feedback... I have done some pretty trivial, small websites with html/PHP,
4
1745
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
1707
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 more or less the same thing over and over again. So its feels like I need to get into class programming with all its attendant benefits. However my biggest problem is a conceptual one. I just can't get my head around defining suitable classes, how...
6
2119
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
17
2714
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
11
2363
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 similar to a database or XML representation. Rules to modify the stored data will be executed, then the data will be transformed into an output format. Think something similar to FormatA -> XML -> Manipulate XML -> FormatB
0
2509
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 don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
0
896
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 python (ie they still use either Excel, S-PLUS/R, or Matlab only). I need to dump the results into text such that these users can do further processing, and I am looking for ideas on good ways to do this. Note that formatting and displaying simple...
6
1888
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 called: tempInput.txt, and then the information is calculated, and it is output on a different file called tempOutput.txt. The tempInput.txt containg those numbers as follows:
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.