Re: Need help with good OO design

Discussion in 'C++' started by Victor Bazarov, Jun 26, 2003.

  1. "Joey Mukherjee" <> wrote...
    > I have a problem doing good OO design and seek some opinions of how
    > to do it better.
    >
    > I have one class that is my base class called Telemetry. It opens a
    > file, and reads the first few bytes (the header) of a file. Based on
    > what it just read, I determine what kind of derived class I have
    > (either Science or Housekeeping) and create my dervived class via
    > copy constructor.
    >
    > I do work with the derived class which includes reading more data
    > from the file and then I need to read a new header (i.e. the base
    > class stuff).
    >
    > The code is as follows:
    >
    > while (telemetry->Good ()) {
    >
    > telemetry->ReadNextPacket ();
    >
    > if (telemetry->IsScience ()) {
    > science->Copy (telemetry);
    > science->ReadNextPacket ();
    > // do science specific stuff
    > } else {
    > housekeeping->Copy (telemetry);
    > housekeeping->ReadNextPacket ();
    > // do housekeeping specific stuff
    > }
    > }
    >
    > delete science;
    > delete housekeeping;
    > delete telemetry;
    >
    > Since the file pointer is essentially shared with all three classes, the
    > ifstream * defined in the base pointer will get deleted too many times.
    >
    > What is the best way to handle this problem? I cannot move the specific
    > stuff into the classes which the FAQ suggests.


    Too little information. For example, why can't you create the
    file stream outside the 'telemetry' which would allow you to stop
    worrying about destroying it more than once? Why don't you put
    reading of the header outside the loop?
    -------------------------------------------------------
    Telemetry *pt = new Telemetry(somefile);
    pt->readHeader();
    if (Science::isCorrectHeader(pt->getHeader()))
    {
    Telemetry *was = pt;
    pt = new Science(*pt);
    delete was;
    }
    else if (Housekeeping::isCorrectHeader(pt->getHeader()))
    {
    Telemetry *was = pt;
    pt = new Housekeeping(*pt);
    delete was;
    }
    else
    throw "Incorrect header";

    while (pt->Good())
    {
    if (pt->readStuff()) // those should be
    pt->doStuff(); // virtual functions overridden
    // in derived classes
    }

    delete pt;
    -------------------------------------------------------
    The code above strives to detach the knowledge of what is
    "Science" and what is "Housekeeping" from "Telemetry". IOW,
    the base class should NOT know about the classes that derive
    from it. Try following that rule and see what develops(tm).

    Victor
     
    Victor Bazarov, Jun 26, 2003
    #1
    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. SpamProof
    Replies:
    3
    Views:
    670
    SpamProof
    Dec 1, 2003
  2. David Rubin

    Re: Need help with good OO design

    David Rubin, Jun 26, 2003, in forum: C++
    Replies:
    0
    Views:
    743
    David Rubin
    Jun 26, 2003
  3. Bob Hairgrove

    Re: Need help with good OO design

    Bob Hairgrove, Jun 26, 2003, in forum: C++
    Replies:
    0
    Views:
    737
    Bob Hairgrove
    Jun 26, 2003
  4. John Gabriele

    help: good sturdy C procedural program design

    John Gabriele, Apr 24, 2004, in forum: C Programming
    Replies:
    2
    Views:
    514
    John Gabriele
    Apr 24, 2004
  5. Kiran
    Replies:
    0
    Views:
    347
    Kiran
    Jul 3, 2007
Loading...

Share This Page