追加のクラスの機能構成によ
-
28-09-2019 - |
質問
あるとしましょう抽象クラス Element
からの授業 Triangle
や Quadrilateral
ているからです。
仮にもかかわらず、これらのクラスとの併用補間方法に依存する形あるものにしていく所存です。なので、基本的には今作の抽象クラス InterpolationElement
からいくつかの基本的な性質を導い InterpolationTriangle
や InterpolationQuadrilateral
.
そして、補間機能 Triangle
や Quadrilateral
クラスを追加しましたconstデータ参照会員クラス Element
タイプ InterpolationElement
, であることが
class Element
{
public:
Element(const InterpolationElement& interp);
const InterpolationElement& getInterpolation() const;
private:
const InterpolationElement& interpolation;
};
その作成方法として記述されるスコットMeyersに有効なC++)instanciate地元の静的オブジェクトのクラス InterpolationTriangle
として
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}
}
解決
のクラスとの併用補間 方法.なぜその方法にする必要がありシングルトンオブジェクト?のシングルトンがここに見えても問題がある。
class Element
{
public:
virtual double interpolationMethod1(...) = 0;
:
virtual double interpolationMethodN(...) = 0;
};
class Triangle : public Element
{
public:
virtual double interpolationMethod1(...) { // interpolation for triangle }
:
virtual double interpolationMethodN(...) { // interpolation for triangle }
}
また、歓迎というわけで!
他のヒント
これを彷彿とさせる問題があったか回答 こちらの.同じ考えの分離データコンテナの戦略に関する
あはさすがに調度品等は古くなっ課題をご提案:を追加する補間と補間関係の方法を基底クラスだけを変更しのコンストラクタ...
てゆきたいと思いまだこのようになっておりますが、ここではどのようにするべきなので
class Element
{
public:
private:
// similar signature to a `clone` method
virtual InterpolationElement* interpolation() const = 0;
};
class Triangle
{
public:
private:
virtual InterpolationTriangle* interpolation() const
{
return new InterpolationTriangle();
}
};
ある2つの利点はこちら
- でないとの互換性をとるために、コンストラクタのそれぞれの派生物の
- この戦略のオブジェクトがなくなっ
const
, ることができるの維持状態の計算...のように流れるオブジェクト補間。
しかし、この地が変更に Element
クラスは、その由来。ねん;)?
りのさるとしあきだいえっと時間(一度)を求めるデザインパターン: Visitor
.
と少し違うからこの戦略発想に依拠し、ダブルに派遣。しかしなどができます。itunesの階層 Element
s回( accept
方法)として追加する多くの業務をすることが出来ます。ことになる。
きゃっとテンプレート。まず、トップクラスです。
class Element {
public:
virtual void calculate() const = 0;
};
...その後はまた、クラスの階層にあるテンプレートを作成します。テンプレートできないトップレベルのクラスは、テンプレートと異なるパラメータが異なる。という考え方ですし、補間においては、typeパラメータの要素となります。
template <typename Interpolation>
class Element_Impl : public Element {
protected:
Interpolation m_interpolation;
};
および補間。予告な兄弟姉妹がいらなくなる。
class InterpolationTriangle {
public:
double interpolate(double a, double b) const {
std::cout << "interpolation triangle" << std::endl;
}
};
class InterpolationQuadrilateral {
public:
double interpolate(double a, double b) const {
std::cout << "interpolation quadrilateral" << std::endl;
}
};
その要素の主な手順です。
class Triangle : public Element_Impl<InterpolationTriangle> {
public:
void calculate() const {
m_interpolation.interpolate(1.0, 2.0);
}
};
class Quadrilateral : public Element_Impl<InterpolationQuadrilateral> {
public:
void calculate() const {
m_interpolation.interpolate(2.0, 3.0);
}
};
int main() {
const Element &a = Triangle();
const Element &b = Quadrilateral();
a.calculate();
b.calculate();
}
概要:
- 簡単にスイッチ補間のクラスの各要素が必要です。
- なのにダブルのvtableのアクセス(最初の要素の計算を用InterpolationElementのintepolate方法としてのMatthieuの例です。各要素を知ってコンパイル時間補間のクラスです。
- Element_Implは醜いビットが、これにより省からcopypasta.きもの実施により補間方法の包み
- http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
一方法は、静的な方法を定義するラッパーでElement_Impl-ないでいただけます。
class Element {
public:
virtual void calculate() const = 0;
};
template <typename Interpolation>
class Element_Impl : public Element {
protected:
void interpolate(double, double) const {
Interpolation::interpolate(1, 1);
}
};
class InterpolationTriangle {
public:
static double interpolate(double a, double b) {
std::cout << "interpolation triangle" << std::endl;
}
};
class InterpolationQuadrilateral {
public:
static double interpolate(double a, double b) {
std::cout << "interpolation quadrilateral" << std::endl;
}
};
class Triangle : public Element_Impl<InterpolationTriangle> {
public:
void calculate() const {
interpolate(1.0, 2.0);
}
};
class Quadrilateral : public Element_Impl<InterpolationQuadrilateral> {
public:
void calculate() const {
interpolate(2.0, 3.0);
}
};
int main() {
const Element &a = Triangle();
const Element &b = Quadrilateral();
a.calculate();
b.calculate();
}
かたがたには自分の GoFデザインパターンをお客様
だからこそ理解の問題は、このパターンを考えを正確に解決します。
それぞれの訪問者オブジェクトを定義します補間手法やアルゴリズムの申請をすオブジェクトです。
このように要素のクラスの生えていないんでそれぞれの新しいfunctionnality.一度、お客様のパターンを豊かにfunctionnalityに触れることなく、基底クラス定義で設定します。