Indeed, it does - to some degree. That's why there is another pattern to achieve effectively the same, but without violating the SRP: the strategy pattern, used in a "context class" like this:
class Context
{
private:
IStrategy *_strategy;
public:
void func()
{
_strategy->_func1();
_strategy->_func2();
}
};
However, as always when making design decisions, this is a trade-off: using the strategy pattern produces a little bit more boilerplate code, and the virtual functions now must be made public
instead of protected
, so it loosens encapsulation to some degree. You will have to trade "YAGNI" vs. "SRP" here.
For small classes or template methods, the "template method pattern" is totally sufficient, for bigger ones, "strategy" may be the better choice. And sometimes, using no pattern at all is the best solution of all.