Element
クラスTriangle
とQuadrilateral
が派生する抽象クラスがあるとします。
ただし、これらのクラスは、要素の形状に依存する補間メソッドと組み合わせて使用されると仮定します。InterpolationElement
したがって、基本的には、派生元の抽象クラスを作成しInterpolationTriangle
ますInterpolationQuadrilateral
。
次に、Triangle
およびクラスに補間機能を含めるために、タイプのQuadrilateral
クラスにconst-referenceデータメンバーを追加します。つまり、次のようになります。Element
InterpolationElement
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}
}