Re: dynamic_cast is ugly!
In article <ac86eb66-ae72-4d2c-adc7-38304d387cd6
@y77g2000hsy.googlegroups.com>, daniel_t@earthlink.net says...
[ ... ]
There are any number of ways to do it, Dave Mikes mentioned one,
another would be to have a virtual function in Shape that lets Shapes
know that a "change rectangles to yellow" request has been made.
I would posit that (at least as stated) this would constitute quite a
poor design. Rather than "change rectangles to yellow", the request
should be dealt with much more abstractly -- something more like
"highlight interfaces", using a relatively abstract description of the
desired action instead of directly describing its physical
manifestation.
Although I can't say I've seen a use for this specifically in UML, let's
assume for the moment that it really was something you wanted. In that
case, I think stepping through the collection of all the objects and
setting the interfaces to highlighted is only a minor improvement over
stepping through them and setting rectangles to yellow.
Instead, if we want to be able to highlight all the interfaces (or
whatever) we'd share the "highlighted" vs. "normal" state (or perhaps
the current color) among all objects of that type:
struct UML_object {
int x_, y_;
virtual void draw(surface_t &) = 0;
};
class UML_interface : public UML_object {
static color_t color;
public:
static void highlight(bool state=true) {
static color_t colors[] = {
RGB(0,0,0),
RGB(255, 0,0)
};
color = colors[state];
// code to force re-draw goes here.
}
square(int x, int y) : x_(x), y_(y) {}
virtual void draw(surface_t &s) {
// draw "interface" on specified surface
}
};
class UML_class : public UML_object {
public:
UML_class(int x, int y) : x_(x), y_(y) {}
virtual void draw(surface_t &s) {
// draw "class" on specified surface
}
};
color_t UML_interface::color;
This way we don't need separate containers OR a dynamic_cast to
highlight all your UML_interface objects -- instead, you call:
UML_interface::highlight();
and they all highlight. To change them all back to normal, you call:
UML_interface::highlight(false);
IMO, if you want shared state, it's better to create real shared state
than to force all objects of that type to the same state, independently
of each other.
--
Later,
Jerry.
The universe is a figment of its own imagination.