• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Patterns: Strategy Pattern and Template Method Pattern.

 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't distinct the two patterns. It seems that the two patterns it the same. Maybe i don't have the image down for them. I couldn't think of a case to use Template Medthod instead of a Strategy. Can any show me some real world problem using Template Medthod or Strategy? I think that will help alot.
I know the different in definition but not on when to use them.
I know Strategy is intented to allow different rules of algorithms depend upon the context in which they occur. Selecting an alogorithm that needs to be applied depends up the client.
i know Template Method pattern define the skeletion of algorithm in an operation. Redefine the alogorithm in subclasses without changing the algorithm's structure.
When you are supposed to use Template Method patterns but switching to Strategy it still work. And the other way around.
I know it is not like that but I couldn't distinct it.

thanks.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, give me a chance to explain.
Template Method
-----------------
The idea here is 'subclasses don't override the main template method, they simply override some of the steps in the alogorithm, which are some primitive methods called in template method.'
so basic idea is to avoid the call super.method() in overriding methods. and to capture all the steps of an algorithm in one method in the abstract class.
Strategy
---------
this is for pluggable algorithms, if u put different algorithms in diffrent classes, it will be easy to switch between them in the client code where u use them.
let me know if u r not clear.
pl. read more here http://ciol.com/content/technology/sw_desg_patt/
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Avijeet,
It is very clear.
Do you have a real world example you can show me?
either using Strategy or Template Method. I think i can't get my mind right to when to use either one. For example, if i have to connect to a Database (SQL or Orcale). Then I have two connect algorithms (SQL and orcale) right? Strategy fits it but Template Method also does. Which one do you think it is the right one to use?
i can have a Stractegy_database abstract class and have SQL extend it and Orcale exit it.
or
i can have a Template Method abstract class.
In it i have a access method using two abstract helper method. Then, i have a SQL extend Template method abstract class and implement the helper method for SQL and
i have a Orcale extend template method abstract class and implement the helper method for Orcale. The client class use the access method in abstract class to access either database.
thanks again.

[This message has been edited by FEI NG (edited November 15, 2001).]
 
Avijeet Dash
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your case seems a perfect example of strategy not template method. In case of template method there sud be a mjor chunk of functionality which is shared across sub classes. s all that can be put in the template method which needs no override.
U can also use template method while using strategies. Its just a method which forces the subclasses to use it.

 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"U can also use template method while using strategies. Its just a method which forces the subclasses to use it."
can u give an example on that??

reply
    Bookmark Topic Watch Topic
  • New Topic