James Kanze said:
So explain the alternatives, and show how they're better. For
the moment, all you've said is "bad design", with no explination
as to why, or what would be better. Sort of like a child at a
playground.
A related issue came up at work today...
Our framework has a class "Graphic", we have several classes derived
from it including "Image" and "Animation".
Graphics have rotate functionality, but they rotate about their center,
and what I needed was to be able to rotate the graphic about any
arbitrary point along the vertical center of the graphic. This meant
doing some math to change the x, y of the graphic as I was rotating it.
In addition, Graphics have a "bool hit( int x, int y ) const" function.
If the 'x' and 'y' are in the hit area of the graphic, then it returns
true. This function also needs to be modified so that it returns true if
a particular region of the graphic was hit based on the current rotation.
(To help picture the situation, imagine a picture of a crank. Normally,
for such an image, the fulcrum would be at the center of the crank, and
the hit region would be anywhere on the image. What I needed was for the
fulcrum to be at one end of the image, and the hit region to be at the
other.)
So, in my estimation, I need a decorator that changes what the rotate
and hit functions do.
class RotateableGraphic : public Graphic {
Graphic* graphic;
// other data to keep track of the sweep radius.
public:
RotateableGraphic( Graphic* g ): graphic( g ) { }
void rotate( int degrees ) {
// do math that rotates 'graphic' and changes its 'x' and 'y'
}
bool hit( int x, int y ) const {
// do the math to figure out if 'x' and 'y' are in the hit region
}
};
Part of the problem is that I also want to be able to call the
additional functions in Image and Animation even after adding it to the
RotateableGraphic.
It seems that some here would argue that the best thing to do would be
to provide a "getGraphic" function and then dynamic_cast it in order to
access that extra functionality. What would you do?