I'm using template method pattern in my project like following
class Template
{
public:
void algorithm();
{
A();
B();
}
private:
virtual void A()=0;
virtual void B()=0;
}
I have some subclasses implement method A & B in different ways.
But now I need a new class Template2 to implement a slightly different algorithm.
class Template2
{
public:
void algorithm();
{
A();
B();
C();
}
private:
virtual void A()=0;
virtual void B()=0;
void C()
{
//some stuff
A();
//some stuff
B();
}
}
C is identical along all subclasses, so I do not make it virtual.
Right now I create a new inheritance hierarchy based on Template2, but this seems stupid because I have to copy and paste every subclasses's code in this new hierarchy.
Is there anyway to do it in a more elegant way?
Aucun commentaire:
Enregistrer un commentaire