In an interview i was asked about the key strategies of template method pattern. I answered Inheritance.Is this the right answer? If anyone can direct me in the right direction? Thanks in advance.
Asked
Active
Viewed 43 times
-2
-
1This question appears to be off-topic. It probably belongs on http://programmers.stackexchange.com – Jason Down Sep 30 '14 at 15:18
-
The key strategies means "what you have to worry about implementing this". Are you sure they didnt ask you about the key advantages? If so your answer "Inheritance" would make kind of sense if not i does not. Notice that Inheritance is not an advantage on its own either. – user2504380 Sep 30 '14 at 15:20
-
@user2504380 they asked about key strategies. – user2835477 Sep 30 '14 at 18:06
1 Answers
0
Template Method is a pattern that can provide an excellent way of keeping logic centralised in a base class, while derivatives fill-in the specific details that only they know about. So I would argue that one of its key strategies is to avoid repetition of logic.
However, it has its downsides. Here's just one article that details some of them.
Like all design patterns, it has a time and a place but must be used appropriately.

David Osborne
- 6,436
- 1
- 21
- 35
-
-
Say you have a base class and a number of derivatives that model a concept. If that concept has a behaviour that differs slightly in each of the derivatives, you have a choice in how you implement this. Each derivative can just override a method in the base class and then provide their specific implementation. However, if each implementation does largely the same thing with just some derivative-specific bits you'll have a lot of duplication. You can push the shared logic into the base class, removing the need to replicate it across the derivatives. Then the template method does the rest. – David Osborne Oct 01 '14 at 09:44