S
SophistiCat
Hi,
I am working on a computational program that has to read a number of
parameters (~50) from an input file. The program contains a single class
hierarchy with about a dozen member-classes or inherited classes, each of
which needs some subset of those input parameters. The classes may
individually perform some input validation, and even determine which
parameters are to be read next. Currently, each class performs its own
file input. This doesn't satisfy me, mainly because the program is hard
to read and maintain when the input is dispersed among many different
classes.
What would be a good strategy for organising input in this case?
My first impulse was to at least "outsource" the actual file input into a
separate class, which also keeps track of the current position and
context within the input file and can do meaningful error reporting. The
other classes utilise the input class interface to get the parameters
that they need. But that doesn't quite solve the problem, in my view.
Should I have a class that reads the input file in its entirety, and then
makes the results available to other classes? I see too problems(?) with
that:
- The parameter sequence is not entirely linear. Depending on context,
the input file may contain some parameters and not others. The logic is
currently determined by the classes that read their subsets of input
parameters. I would have to transfer that logic to the input-reading
class, which doesn't sound right.
- If a change in some class places a different requirement on input data,
this change would also have to be implemented in the input-reading class.
This seems to go against the encapsulation principle. Trouble is likely
if the input-class is not synchronised with all the other classes that
require input.
- Individual classes only need access to a subset of the input
parameters, not all of them. Is there some elegant way of giving each
class access only to those parameters that it needs, and not the entire
"catalogue"?
Any ideas?
Thanks.
P.S. I realise that this is not specifically a C++ question, but some
solutions may involve features that are specific to C++.
I am working on a computational program that has to read a number of
parameters (~50) from an input file. The program contains a single class
hierarchy with about a dozen member-classes or inherited classes, each of
which needs some subset of those input parameters. The classes may
individually perform some input validation, and even determine which
parameters are to be read next. Currently, each class performs its own
file input. This doesn't satisfy me, mainly because the program is hard
to read and maintain when the input is dispersed among many different
classes.
What would be a good strategy for organising input in this case?
My first impulse was to at least "outsource" the actual file input into a
separate class, which also keeps track of the current position and
context within the input file and can do meaningful error reporting. The
other classes utilise the input class interface to get the parameters
that they need. But that doesn't quite solve the problem, in my view.
Should I have a class that reads the input file in its entirety, and then
makes the results available to other classes? I see too problems(?) with
that:
- The parameter sequence is not entirely linear. Depending on context,
the input file may contain some parameters and not others. The logic is
currently determined by the classes that read their subsets of input
parameters. I would have to transfer that logic to the input-reading
class, which doesn't sound right.
- If a change in some class places a different requirement on input data,
this change would also have to be implemented in the input-reading class.
This seems to go against the encapsulation principle. Trouble is likely
if the input-class is not synchronised with all the other classes that
require input.
- Individual classes only need access to a subset of the input
parameters, not all of them. Is there some elegant way of giving each
class access only to those parameters that it needs, and not the entire
"catalogue"?
Any ideas?
Thanks.
P.S. I realise that this is not specifically a C++ question, but some
solutions may involve features that are specific to C++.