class AbstractShape;
class RectangularShape : public AbstractShape
{
void setWidth(double v);
void setLength(double v);
};
class CircleShape : public AbstractShape
{
void setRadius(double v);
};
class PolygonalShape : public AbstractShape
{
void addPoint(Point p);
};
class Element
{
protected:
AbstractShape* _shape; //RectangularShape, PolygonalShape or CircleShape
};
我想在元素中创建方法来修改元素的形状_shape(即如果形状是矩形的,我必须能够改变长度和宽度,否则我必须能够添加一个点到多边形形状等)。修改抽象属性的界面
例如,我不能声明setLength方法,因为setLength只是_shape是RectangularShape时的含义。一种解决方案是在RectangularElement,PolygonalElement,CircularElement中对Element进行子类化,但我想避免这种解决方案。 你看到另一种方式吗?
访客模式? – StoryTeller
一个矩形也应该是一个多边形,考虑添加一个间接层来变形多边形 – Curious
@ user1482030所以你想要一个胖接口? – Curious