C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie: How to organize inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
allnor@tele.ntnu.no
Guest





PostPosted: Wed Apr 20, 2005 9:10 am    Post subject: Newbie: How to organize inheritance Reply with quote



Hi all.

I am implementing a file access program, that (hopefully)
will be used for several different file formats. The formats
are generally of the same structure,

File Header
Data Block Header #1
Data Block #1
Data Block Header #2
Data Block #2
etc.

The file formats differ in the binary formats of each of
these blocks. So lots of the basic operations are the same
for each file format. Only some low-level data access
routines vary between the file formats.

I don't want to duplicate basic implementations, so I
wonder how best to organize my code. What I have in
mind is something like this:

class Basics {
private:
// buffering, low-level basic I/O to file

protected:
// format-specific virtual methods that need
// to be implemented in derived classes
virtual void readLowLevelData();

public:
// interface methods mutual between derived classes
void readData();
};

class FormatA : protected Basics {
private:
// implement details specific to file format
readLowLevelData();
};

class FormatB : protected Basics {
private:
// implement details specific to file format
readLowLevelData();
};

//////////////////////////////////////////////////////
void Basic::readData()
{
readLowLevelData();
}
//////////////////////////////////////////////////////

My question is whether the method Basic::readData()
can be implemented as indicated. In the Basic class,
the method readLowLevelData() is not available, it
is only implemented in derived classes.

In the FormatX classes, the call to the method readData()
is inherited from the Basic class, but the implementational
details are specific to each class.

Is this a good way of doing things? If not, why?

Thanks in advance for any input and suggestions!

Rune


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
R.F. Pels
Guest





PostPosted: Wed Apr 20, 2005 4:49 pm    Post subject: Re: Newbie: How to organize inheritance Reply with quote



[email]allnor (AT) tele (DOT) ntnu.no[/email] wrote:

Quote:
I am implementing a file access program, that (hopefully)
will be used for several different file formats. The formats
are generally of the same structure,

File Header
Data Block Header #1
Data Block #1
Data Block Header #2
Data Block #2
etc.

The file formats differ in the binary formats of each of
these blocks. So lots of the basic operations are the same
for each file format. Only some low-level data access
routines vary between the file formats.

I don't want to duplicate basic implementations, so I
wonder how best to organize my code.

In the FormatX classes, the call to the method readData()
is inherited from the Basic class, but the implementational
details are specific to each class.

Hmmm. In this case, if you want to keep things easily extensible, it's best
to use delegation in combination with a factory instead of inheritance to
act on the data blocks. Meaning, you instantiate a specific class for a
specific data block based on the content of the header.

Let me explain a bit (while making some assumptions). There is one object
that reads the file, lets call it a FileReader. At the start, it knows to
instantiate a FileHeader object. That object digests the information in the
file header. Then, the FileReader object uses the information in the
FileHeader object to access the first data block header, or the part in the
data block header that is the same for all data block headers. From that,
it can instantiate a DataBlockHeaderXXX object which can digest the whole
data block header. Based on that, a DataBlockXXX is instantiated that reads
and digests the data block itself. Etcetera. Creation of the
DataBlockHeaderXXX objects and DataBlockXXX objects is delegated to a
DataBlockHeaderFactory and a DataBlockFactory.

If all data block headers are the same, then you don't need the factory or
the DataBlockHeaderXXX objects. A single DataBlockHeader class suffices in
that case. Let's assume this is the case. Then you have a FileReader class,
a FileHeader class, a DataBlockHeader class. Also, you need to create a
DataBlockFactory class that knows which DataBlockXXX object to instantiate,
and an inheritance tree with DataBlockXXX classes all derived from
DataBlock. What you additionally can do is register the DataBlockXXX
classes with the factory and invent a method to match information in the
block header to the correct DataBlockXXX class.

--
Ruurd
..o.
...o
ooo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rune Allnor
Guest





PostPosted: Thu Apr 21, 2005 12:06 pm    Post subject: Re: Newbie: How to organize inheritance Reply with quote




R.F. Pels wrote:
Quote:
allnor (AT) tele (DOT) ntnu.no wrote:

I am implementing a file access program, that (hopefully)
will be used for several different file formats. The formats
are generally of the same structure,

File Header
Data Block Header #1
Data Block #1
Data Block Header #2
Data Block #2
etc.

The file formats differ in the binary formats of each of
these blocks. So lots of the basic operations are the same
for each file format. Only some low-level data access
routines vary between the file formats.

I don't want to duplicate basic implementations, so I
wonder how best to organize my code.

In the FormatX classes, the call to the method readData()
is inherited from the Basic class, but the implementational
details are specific to each class.

Hmmm. In this case, if you want to keep things easily extensible,
it's best
to use delegation in combination with a factory instead of
inheritance to
act on the data blocks. Meaning, you instantiate a specific class for
a
specific data block based on the content of the header.

Let me explain a bit (while making some assumptions). There is one
object
that reads the file, lets call it a FileReader. At the start, it
knows to
instantiate a FileHeader object. That object digests the information
in the
file header. Then, the FileReader object uses the information in the
FileHeader object to access the first data block header, or the part
in the
data block header that is the same for all data block headers. From
that,
it can instantiate a DataBlockHeaderXXX object which can digest the
whole
data block header. Based on that, a DataBlockXXX is instantiated that
reads
and digests the data block itself. Etcetera. Creation of the
DataBlockHeaderXXX objects and DataBlockXXX objects is delegated to a
DataBlockHeaderFactory and a DataBlockFactory.

If all data block headers are the same, then you don't need the
factory or
the DataBlockHeaderXXX objects. A single DataBlockHeader class
suffices in
that case. Let's assume this is the case. Then you have a FileReader
class,
a FileHeader class, a DataBlockHeader class. Also, you need to create
a
DataBlockFactory class that knows which DataBlockXXX object to
instantiate,
and an inheritance tree with DataBlockXXX classes all derived from
DataBlock. What you additionally can do is register the DataBlockXXX
classes with the factory and invent a method to match information in
the
block header to the correct DataBlockXXX class.

Thanks, there are some very interesting ideas here.

I may have been a bit unclear with my question, though. The binary
formats are fixed for the various blocks, once the file format
is specified. What I want, is a generic class that handles all the
nitty-gritty details of handling data files, and where I only have
to implement the low-level format-specific details when I want to
add a new file format to my system.

Rune


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Apr 21, 2005 4:50 pm    Post subject: Re: Newbie: How to organize inheritance Reply with quote


R.F. Pels wrote:
Quote:
allnor (AT) tele (DOT) ntnu.no wrote:

I am implementing a file access program, that (hopefully)
will be used for several different file formats. The formats
are generally of the same structure,

File Header
Data Block Header #1
Data Block #1
Data Block Header #2
Data Block #2
etc.

The file formats differ in the binary formats of each of
these blocks. So lots of the basic operations are the same
for each file format. Only some low-level data access
routines vary between the file formats.

I don't want to duplicate basic implementations, so I
wonder how best to organize my code.

In the FormatX classes, the call to the method readData()
is inherited from the Basic class, but the implementational
details are specific to each class.

Hmmm. In this case, if you want to keep things easily
extensible, it's best to use delegation in combination with a
factory instead of inheritance to act on the data blocks.
Meaning, you instantiate a specific class for a specific data
block based on the content of the header.

It depends. What it boils down to is a choice between the
strategy pattern (aka delegation) and the template pattern; his
proposed implementation uses the template pattern.

Generally, the strategy pattern results in weaker coupling
which, all other things being equal, is better. (But all other
things aren't always equal.) It also allows changing the
algorithms dynamically, although that isn't an issue here (and
it usually isn't used for that reason). In the case of C++,
there is an additional advantage in certain cases -- the
strategy pattern works during construction, whereas the template
pattern only becomes fully effective once the constructors have
finished running.

On the other hand, the template pattern allows the
implementation of the strategy easy access to the base class.
In some cases, this is useful -- in his case, for example, the
base class handles buffering and the physical IO, which his
derived implementation might want to use.

Another alternative might be to factor the phyical reading and
bufferization out into a totally separate class (called
filebuf?), passing it as a parameter to the functions in the
strategy.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
R.F. Pels
Guest





PostPosted: Fri Apr 22, 2005 2:25 pm    Post subject: Re: Newbie: How to organize inheritance Reply with quote

Rune Allnor wrote:

Quote:
Thanks, there are some very interesting ideas here.

:-)

Quote:
I may have been a bit unclear with my question, though. The binary
formats are fixed for the various blocks, once the file format
is specified. What I want, is a generic class that handles all the
nitty-gritty details of handling data files, and where I only have
to implement the low-level format-specific details when I want to
add a new file format to my system.

If I understand you correctly, the content of header blocks and data blocks
in the file are similar in structure. Even then, if you want to add
additional formats in different files it is still a good thing to parcel
out the per-file handling to separate classes. If you do, that opens up
additional possibilities in the flexibility of deploying those handlers,
for example in some type of plugin structure where you detect shared
objects (DLLs if you wish) that can handle a particular type of file:

read header
Quote:

V

determine file type
Quote:

V

append postfix
Quote:

V

load shared object/DLL
Quote:

V

instantiate object
Quote:

V

parse blocks

--
Ruurd
..o.
...o
ooo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.