Hello,
I have a program that reads from a file at several different places. I
want to find when the file hits the EOF. At this point I want to tell
the calling method that this has happened so that the program can move
onto the next phase.
My class hierarchy is something like:
main() -> class1 -> class1A -> class1a
-> class1B
-> class1C
-> class2 -> class2A -> class2a
-> class3
Class1a, class1B and class1C all read the same file.
When the file is finished with, control must be passed to class3.
At present I pass this message by using 'return 1;' from the read
method of classes 1a, 1B, 1C and checking the status of this method
using for example:
if (class1a.readMethod() == 1) return 1; // called by
class1A.someMethod();
The problem I am facing is that the calling class (class1A above),
which checks the status, is not the same class as that which
determines whether the program continues beyond the file read
(class1).
Therefore, as shown below, I have to pass this message up several
classes from the read method in the bottom class to the control method
in the top class, i.e. continually returning 1 if the method of the
subclass returned 1.
Also, since I read the file from several different classes, I must do
this message passing from several points through the hierarchy.
main()
{
while(true)
{
if (class1.someMethod() == 1) break;
}
class3.someMethod();
};
int class1.someMethod()
{
if (class1A.someMethod() == 1) return 1;
};
int class1A.someMethod()
{
if (class1a.someMethod() == 1) return 1;
if (class1B.someMethod() == 1) return 1;
if (class1C.someMethod() == 1) return 1;
};
As you can see, this is very messy.
Can anyone suggest a better design for this kind of system?
Perhaps a error handling class, called if EOF is reached, which
returns control to a strategic point? I just can't see it...
thanks
Fred