2017-05-19 4 views
0

私は以下のクラス階層を持っています。次のような階層内の複数のクラスを紹介2人の子供はF()の同じ実装を持ち、他の2人の子供はFの異なる実装を持っています.4つはすべて同じクラスから派生します

class Base 
{ 
virtual void F(); 
} 

class Derived1 : public Base 
{ 
    void F() { 
     cout<< " one implementation"<<endl; 
    } 
} 
class Derived2 : public Base 
{ 
    void F() { 
     cout<< " one implementation"<<endl; 
    } 
} 
class Derived3 : public Base 
{ 
    void F() { 
     cout<< " other implementation"<<endl; 
    } 
} 
class Derived4 : public Base 
{ 
    void F() { 
     cout<< " other implementation"<<endl; 
    } 
} 

は、他の兄弟よりも同じ実装を持つ少数の兄弟の上記のユースケースをよりよく取り組むことができる場合、私は思っていた問題に

class Base 
    { 
    virtual void F(); 
    } 

class Base1 : public Base { 
    void F() { 
      cout<< " one implementation"<<endl; 
     } 
} 

class Base2 : public Base { 
    void F() { 
      cout<< " other implementation"<<endl; 
     } 
} 

    class Derived1 : public Base1 
    { 

    } 
    class Derived2 : public Base1 
    { 

    } 
    class Derived3 : public Base2 
    { 

    } 
    class Derived4 : public Base2 
    { 

    } 

を解決するように見えますか?私がここで欠けている明らかなデザインパターンはありますか?

私が欲しい唯一の機能がFであれば、構図で解決できるかもしれません(下記参照)。私はそれをすることが良いアイデアかどうかは分かりません。

構図

class Base 
    { 
    virtual void F(); 
    } 

class Base1 : public Base { 
    void F() { 
      cout<< " one implementation"<<endl; 
     } 
} 

class Base2 : public Base { 
    void F() { 
      cout<< " other implementation"<<endl; 
     } 
} 

    class Derived1 
    { 
     Base * Fer; 
     Derived1() 
     { 
     Fer = new Base1(); 
     } 
    } 
    class Derived2 
    { 
     Base * Fer; 
     Derived1() 
     { 
     Fer = new Base1(); 
     } 
    } 
    class Derived3 
    { 
     Base * Fer; 
     Derived1() 
     { 
     Fer = new Base2(); 
     } 
    } 
    class Derived4 
    { 
     Base * Fer; 
     Derived1() 
     { 
     Fer = new Base2(); 
     } 
    } 
+0

あなたが単純化されて記述するものかもしれませ。 _Decorator Pattern_は私のために気になります。 –

+0

まあ、実際には、私は正確な問題に直面しており、今私はそれを再設計し、将来の証明にしようと考えています。デコレータパターンありがとう。 – Ravi

+0

はい、これはデコレータを使用して実現できます。 –

答えて

0

私が組成物を使用することになりますが、この方法:

#include <iostream> 

// define an interface 
class Base 
{ 
virtual void F() = 0; 
}; 

// define an implementation framework which pulls in templated 
// components to do the work 
template<class HandleF> 
struct Impl : Base 
{ 
    virtual void F() override { 
     f_handler(this); 
    } 

    HandleF f_handler; 
}; 

struct OneImplementationOfF 
{ 
    template<class Self> 
    void operator()(Self* self) const 
    { 
     std::cout<< " one implementation"<< std::endl; 
    } 
}; 

struct OtherImplementationOfF 
{ 
    template<class Self> 
    void operator()(Self* self) const 
    { 
     std::cout<< " other implementation"<< std::endl; 
    } 
}; 

// now build our classes  
class Derived1 : public Impl<OneImplementationOfF> 
{ 
}; 

class Derived2 : public Impl<OneImplementationOfF> 
{ 
}; 

class Derived3 : public Impl<OtherImplementationOfF> 
{ 
}; 

class Derived4 : public Impl<OtherImplementationOfF> 
{ 
}; 
関連する問題