In both the Whizlabs and EPractize simulators there's a question that amounts to: Person has class that accomplishes task A, but wants to modify it so that it works somewhat differently (in a different country, change in business rules, etc.), which pattern should they use to accomplish this?
When I first encountered the question in Whizlabs I picked Strategy, thinking they could make a concrete strategy for each case and swap in whichever was needed. The answer given was Template Method, with the rationale that since Template Method broke the task into smaller sub-tasks, it made it easy to change the part that needed changing without affecting the algorithm as a whole.
I can see that logic, but Strategy seemed more correct. Then I took EPractize and saw a very similar question and this time I stubbornly stuck with Strategy, and again the test's answer was Template Method, with a very similar rationale.
Can someone convince me that Template Method is *definitely* the best choice here before I go into Part I and shoot myself in the foot by sticking with Strategy?
This is what Quoted in the GOF pattern book for Template Pattern (typed as is).
Define the skelton of an algorithm in an operation, deferring some stepts to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm;s structure.
According to this definition Template is justified as an answer. Strategy replaces the whole algorithm of doing an operation.
As with few other questions in these mock exams, I think the answer to this one also is not that objective in nature- given the 2 close options. The choice of Template or Strategy depends on whether the "complete" algorithm / business logic or just a "part" of it needs to be changed based on some parameter (like �country� in this case). The question does not clarify whether the person wants to change the business rules "completely" or "partly", but the term "somewhat differently" is comparatively close match for "partly change the business rules�. Based on these words used in the question, �Template� pattern looks a closer fit than �Strategy� (again, this is just the closest I feel- but Strategy is also not completely wrong either). On a lighter note- I think the best person to make this decision is that person him/herself who knows �the extent to which� he/she wants to vary the business rules since the creator of the question hasn�t clarified that person�s intentions in the question.
I don't know if I can convince you, but the pragmatic approach before the exam is that "if Sun think Template Method is the best choice, then - for the purpose of passing the exam - it IS the best choice".
Having said that, try looking at it this way:
Strategy means you have to change the entire algorithm (and duplicate the parts you don't want to change) since each strategy is separate from the others. Less code reuse, less manageable code.
Template Method means you only change the parts that varies. More code reuse (using method from superclass instead of duplicating it), more manageable code.
If a class represents this algorithm in a series of steps, with Strategy you would have to implement validateName() and isRegularCustomer() in your subclasses again. But with the template method you have the option of implementing only getCurrency() and showMessage() in a local specific way.
Originally posted by Randy Yarger: [QB]In both the Whizlabs and EPractize simulators there's a question that amounts to: Person has class that accomplishes task A, but wants to modify it so that it works somewhat differently (in a different country, change in business rules, etc.), which pattern should they use to accomplish this? /QB]
When I took the exam (a few days ago), the design pattern questions from the pool that I got were very unambiguous.
If it mentions that there is a sequence of steps then go with template. Otherwise you might favour composition (Strategy) over inheritance IMO.
I wouldn't sweat it. I doubt the real exam will give you such ambiguous choices.
Good luck. [ February 16, 2007: Message edited by: Peter Cook ]