There is a same concept for Strategy Pattern
and Inheritance
, so I can implement Strategy Pattern
with Inheritance
that sounds it is simpler and cleaner than Strategy Pattern
.
Startegy Pattern
:
class IBase
{
public:
virtual void processAction(void *data) = 0; // pure virtual
}
class Worker: public IBase
{
public:
virtual void processAction(void *data)
{
// define logic
}
}
Inheritance
:
class Base
{
public:
virtual void processAction(void *data) {}
}
class Worker: public Base
{
public:
virtual void processAction(void *data) override
{
// define logic
}
}
My question is what is the difference between them? or when should I use Strategy Pattern
or Inheritance
?
Link: Strategy Pattern