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