blue-sky
blue-sky

Reputation: 53916

Strategy versus template

In reading about design patterns I read that the advantage of strategy over template is that it allows you to encapsulate algorithms and call them at runtime whereas template relies on subclassing. But template has the advantage of maximising code re-use through sub-classing. So template is a better pattern applying the DRY principle. But what is to stop using the strategy pattern and within the algorithm implementation use a template pattern to share code that is common between each algorithm or is this a bad idea?

Upvotes: 1

Views: 168

Answers (1)

Anders Abel
Anders Abel

Reputation: 69280

There is nothing preventing combination of those two patterns. Use strategy to make your algorithms selectable and implement them using template method for reusing common parts of the algorithms.

In real life patterns are often combined and a single class can participate in several patterns, having different roles.

Upvotes: 1

Related Questions