(Design question) Formatting objects for file output

Discussion in 'C++' started by alacrite@gmail.com, Jan 14, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Grizlyk Guest

    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".
    Grizlyk, Jan 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chuck amadi
    Replies:
    1
    Views:
    471
    Larry Bates
    Jun 23, 2004
  2. Replies:
    1
    Views:
    571
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 29, 2006
  3. 7stud
    Replies:
    11
    Views:
    661
    Dennis Lee Bieber
    Mar 20, 2007
  4. Maggie
    Replies:
    5
    Views:
    320
    Maggie
    Sep 8, 2009
  5. martin
    Replies:
    2
    Views:
    142
    Ian Wilson
    Apr 18, 2006
Loading...

Share This Page