I have several classes for draw chart,
class ChartBase {
private:
int transparency; //chart attribute
};
class XYChart : public ChartBase{
private:
std::string xAxis;
std::string yAxis;
};
class LineChart : public XYChart
{
private:
int lineWidth;
int lineColor;
};
and then for some reason refactor the code and extract chart attribute to another classes, and the chartBase has a pointer to new class
class StyleBase {
private:
int transparency;
};
class XYStyle : public StyleBase {
private:
std::string xAxis;
std::string yAxis;
};
class LineStyle : public XYStyle {
private:
int lineWidth;
int lineColor;
};
class ChartBase {
protected:
std::shared_ptr<StyleBase> style;
};
the design has some issues
- in derived class such as LineChart, if want to do some thing with lineWidth, color then need to cast the style pointer to LineStyle to access lineWidth, lineColor attribute.
- each ChartBase derived class in its constructor will create an instance of StyleBase as bellow
class ChartBase{
public:
ChartBase() { style = new StyleBase;}
};
class XYChart : public ChartBase{
public:
XYChart() {style = new XYStyle;}
};
class LineChart : public XYChart {
public:
LineChart() { style = new LineStyle;}
};
the style constructor in base class is not necessary in case, but in XYChart is necessary, we used XYChart directly in some place.
is there any better design? or we should not extract them from chart?