Need some design advice
Hi,
So far I have a class hierarchy which looks something like:
template<class T>
class Shape
{
public:
void SetX();
void SetY();
protected:
Shape()
private:
T xPos;
T yPos;
}
template<class T>
class Circle: public Shape<T>
{
public:
void setRadius(T size);
}
template<class T>
class Square: public Shape<T>
{
public:
void setWidth(T width);
void setHeight(T height);
}
As can be seen, all of the objects share a base class which has some
functionality common to all subclasses. The subclasses also have some
new functionality which is not common at all. What I would like to do
is to extend this class hierarchy with a behaviour such as DrawStyle,
which might be SolidLine, DashedLine, Filled, etc. However, I don't
want the drawing code titly coupled to the objects but it should be
able to get some information about the objects being drawn. One way I
considered doing this is by including an addition template parameter so
it would become:
template<class T, class DrawStyle = SolidLine>
class Shape
{
....
}
template<class T, class DrawStyle = Filled>
class Circle: public Shape<T, DrawStyle>
{
....
}
This seems like a reasonable choice to me since I only want to give the
behaviour to class types and not individual objects(something that
never changes once set).
I then have a problem. Since I would like to still treat objects
uniformly using polymorphism and also be able to call the extended
functionality I won't know what to cast the objects to because
DrawStyle differs between class types. Can anyone suggest what I should
do in this situation?
Thanks for any help.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]