S
SasQ
Howdy.
Is it possible to write a class with some of its
public interface being optional and dependant of
the runtime circumstances?
In the "Object oriented design heuristics" book,
chaoter 5.18 Arthur J. Riel has mentioned the same
problem, but he hadn't shown any good solution.
He wrote that there are two solutions:
1. Putting the optional interface in an inherited class.
2. Making the optional obiect contained by reference.
Solution 2 is not convenient for me, because the only
way to return the obiect or nothing is returning the
pointer. So if the optional functionality isn't available,
I'll have to return a null pointer, which may bring to
program crash if the user won't check that pointer but
use it immediately to access methods of the optional
interface. Secondly, returning a pointer to that
optional interface could be an opportunity for an
error if the user would delete it after use. It may
cause a crash if that interface is a part of the
containing object and is used by it.
The solution 1 isn't any better because it's more
static. At runtime I might create either the base class's
object, or the derived class's object, but still I have
to know which one is constructed to use that optional
functionality.
I'm thinking about some way to achieve this better,
because in my program I have some functionality of a class,
which is optional and I can know if it's available only
by checking that at runtime. But I think that allowing
the user to call this optional interface's methods, and then
rewarding the user with an error, isn't a good solution [though
it's commonly used practice], because the object know well that
the optional interface isn't available BEFORE the user might
call it. I would to offer that optional functionality for
the user only in the case that functionality is available,
and if not, I would like to not allow him to call it.
Is there any good solution of that problem in C++?
Maybe some design pattern or sth like that?
Is it possible to write a class with some of its
public interface being optional and dependant of
the runtime circumstances?
In the "Object oriented design heuristics" book,
chaoter 5.18 Arthur J. Riel has mentioned the same
problem, but he hadn't shown any good solution.
He wrote that there are two solutions:
1. Putting the optional interface in an inherited class.
2. Making the optional obiect contained by reference.
Solution 2 is not convenient for me, because the only
way to return the obiect or nothing is returning the
pointer. So if the optional functionality isn't available,
I'll have to return a null pointer, which may bring to
program crash if the user won't check that pointer but
use it immediately to access methods of the optional
interface. Secondly, returning a pointer to that
optional interface could be an opportunity for an
error if the user would delete it after use. It may
cause a crash if that interface is a part of the
containing object and is used by it.
The solution 1 isn't any better because it's more
static. At runtime I might create either the base class's
object, or the derived class's object, but still I have
to know which one is constructed to use that optional
functionality.
I'm thinking about some way to achieve this better,
because in my program I have some functionality of a class,
which is optional and I can know if it's available only
by checking that at runtime. But I think that allowing
the user to call this optional interface's methods, and then
rewarding the user with an error, isn't a good solution [though
it's commonly used practice], because the object know well that
the optional interface isn't available BEFORE the user might
call it. I would to offer that optional functionality for
the user only in the case that functionality is available,
and if not, I would like to not allow him to call it.
Is there any good solution of that problem in C++?
Maybe some design pattern or sth like that?