S
Steve
Hi,
I just know there must be some kind of well-known design pattern here, but
I'll be damned if I can find it...
Let me explain my situation.
I have a hierarchy of classes for a GUI. All derived from class 'Primitive'.
Fine so far.
I want to add styled 'themes' to my framework so objects will be drawn to
look the same for a certain theme. In doing this, I free up the
responsibility of the base classes in knowing how to draw themselves (they
still do other stuff like event handling, etc.), so I can reuse the classes
in other applications, but have them drawn differently.
I'm just stuck trying to come up with the best way to do this.
My solution at the moment (but I'm still not happy with it) is:
Primitive:
raw forwards drawing responsibility to
GetCurrentTheme()->DrawObject( this, GetRect() ).
Because the Theme object doesn't know what derived Primitive is being drawn
at this point, it has to dynamic_cast the base class pointer to find out
what type of object is really being drawn, and draw that particular type of
object.
The thinking behind this was that a 'default' theme could cope with drawing
the most basic primitives - buttons, check boxes, etc. in a default style.
If it gets a primitive it doesn't know about, it draws nothing.
As the framework is extended new types of primitive are added, and, in
particular, specific applications may use their own primitives.
So, as the types of primitive are extended, so too are themes to cope with
drawing them.
It just feels awkward having to use dynamic_cast to work out exactly what
type of object will be drawn, and I can't help but think there must be some
other way to do this.
TIA for any advice. I'll be happy explain more if required.
I just know there must be some kind of well-known design pattern here, but
I'll be damned if I can find it...
Let me explain my situation.
I have a hierarchy of classes for a GUI. All derived from class 'Primitive'.
Fine so far.
I want to add styled 'themes' to my framework so objects will be drawn to
look the same for a certain theme. In doing this, I free up the
responsibility of the base classes in knowing how to draw themselves (they
still do other stuff like event handling, etc.), so I can reuse the classes
in other applications, but have them drawn differently.
I'm just stuck trying to come up with the best way to do this.
My solution at the moment (but I'm still not happy with it) is:
Primitive:
GetCurrentTheme()->DrawObject( this, GetRect() ).
Because the Theme object doesn't know what derived Primitive is being drawn
at this point, it has to dynamic_cast the base class pointer to find out
what type of object is really being drawn, and draw that particular type of
object.
The thinking behind this was that a 'default' theme could cope with drawing
the most basic primitives - buttons, check boxes, etc. in a default style.
If it gets a primitive it doesn't know about, it draws nothing.
As the framework is extended new types of primitive are added, and, in
particular, specific applications may use their own primitives.
So, as the types of primitive are extended, so too are themes to cope with
drawing them.
It just feels awkward having to use dynamic_cast to work out exactly what
type of object will be drawn, and I can't help but think there must be some
other way to do this.
TIA for any advice. I'll be happy explain more if required.