Re: Design question: polymorphism after object creation
Hi,
Alf P. Steinbach wrote:
Instead keep the distinction between URI or
directory entry or whatever, on the one hand, and what's referred to, on
the other hand. They might implement some common interface. But I can't
for the life of me see the point in having e.g. a directory entry
metamorphose into a file it represents.
it is not that easy, since file/directory was only an example. In fact
almost any URI can turn into a container once the server is connected
and some protocol talk has happened.
Also, I really don't see how it can be helpful for the OP's question to
be able to dynamically replace the implemententaion of a method or set
of methods. It's not like he just wants the behavior of methods to
change.
Exactly. Furthermore it is impossible, since the base class must provide
non-copyable and non-swapable objects. First of all the Mutex to protect
the instance.
He wants dynamic switching between different but overlapping
sets of behaviors, which means complexity and ample opportunities for
bugs to creep in and start families.
No more bugs than any other derived class. In fact I only want to
intercept the construction process at the point where the base object is
fully constructed and before the derived class starts to build. I see no
point where this breaks with some OOP rule, at least as long as the base
is not abstract. Unfortunately the language does not provide a feature
to do this, because a cannot call the constructor of a derived class
without invoking the constructor of the base anew.
In fact, I currently try to design the two class solution. But it turns
out, that most of the services have to be placed in the common 'URI
class', because their state must survive the specialization. On the
other side, the specialized type use these services very often. Any
access from the specialized class have to be done through some owner
reference to the common class. This blows up the code a lot making it
rather unreadable.
At runtime it is similar to a virtual base class, so there will be no
much difference.
Marcel