ElementクラスTriangleとQuadrilateralが派生する抽象クラスがあるとします。
ただし、これらのクラスは、要素の形状に依存する補間メソッドと組み合わせて使用されると仮定します。InterpolationElementしたがって、基本的には、派生元の抽象クラスを作成しInterpolationTriangleますInterpolationQuadrilateral。
次に、Triangleおよびクラスに補間機能を含めるために、タイプのQuadrilateralクラスにconst-referenceデータメンバーを追加します。つまり、次のようになります。ElementInterpolationElement
class Element
{
public:
Element(const InterpolationElement& interp);
const InterpolationElement& getInterpolation() const;
private:
const InterpolationElement& interpolation;
};
InterpolationTriangle次に、クラスのローカル静的オブジェクトを次のようにインスタンス化するメソッド(Scott Meyers、Effective C ++で説明)を作成します。
const InterpolationTriangle& getInterpolationTriangle()
{
static InterpolationTriangle interpolationTriangle;
return interpolationTriangle;
}
そのため、クラスTriangleは次のように構成できます。
class Triangle : public Element
{
public:
Triangle() : Element( getInterpolationTriangle() ) {}
};
これが私の質問です:私のクラスに補間メソッドを組み込むために、このアプローチは正しいElementですか?これはプロのシナリオで使用されますか?
(純粋な仮想として)クラスにすべての補間メソッドを直接実装Elementし、派生クラスTriangleとでそれらをオーバーライドすることができますQuadrilateral。ただし、新しい補間機能を改善または実装する必要があるたびに、これらのクラスでそれを実行する必要があるため、このアプローチは面倒に思えます。さらに、このアプローチを使用すると、クラスはどんどん大きくなります(多くのメソッド)。
ヒントやコメントをお聞かせください
前もって感謝します。
追加の詳細:
class InterpolationElement
{
public:
InterpolationElement();
virtual double interpolationMethod1(...) = 0;
:
virtual double interpolationMethodN(...) = 0;
}
class InterpolationTriangle : public InterpolationElement
{
public:
InterpolationTriangle () {}
virtual double interpolationMethod1(...) { // interpolation for triangle }
:
virtual double interpolationMethodN(...) { // interpolation for triangle }
}
class InterpolationQuadrilateral : public InterpolationElement
{
public:
InterpolationTriangle () {}
virtual double interpolationMethod1(...) { // interpolation for quadrilateral}
:
virtual double interpolationMethod1(...) { // interpolation for quadrilateral}
}