• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

strategy and template pattern

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Below is a code for Strategy pattern

interface Strategy
{
void strategyMethod();
}
class Strategy1 implements Strategy
{
public void strategyMethod
(
//implementation of Strategy1
}
}

class Strategy2 implements Strategy
{
public void strategyMethod
(
//implementation of Strategy2
}
}

class Context
{
private Strategy strategy;

Strategy decide(int case)
{
switch (case)
case '1': return new Strategy1() break
case '2': return new Strategy1() break

}
}

Test
{
public static void main(String a[])
{
Context context = new Context();
int value = getStrategy();
Strategy strategy = context.decode(value);
strategy.strategyMethod()
}
}

Isn't this the case of a simple OO polymorphism. what is the big deal abt having this as a named pattern? Isnt is so obvios? or am i missing something here?


And In case of template pattern, instead of interface we have an abstract class called Strategy. rest remains the same.
Is this the only difference between Strategy and Template pattern?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The template pattern aims to define a *workflow* that all subclasses should follow. For example,


Thus, any subclasses that extends the test must implement methodToBeImplemented(), and usually, methodA() is called by another class so that it controls the workflow of each subclasses that must class methodB(), methodC() and methodToBeImplemented() in sequence.

However, in Strategy pattern, the flow seems not being enforced.

Nick
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't go so far as to declare that the Strategy pattern is merely an example of Polymorphism. Yes, it uses polymorphism to achieve a goal, but so do a lot of other patterns. The strategy pattern can be very useful. I always like to use a game playing example. Suppose you wrote a game that played chess. And, you abstracted all of the logic for choosing the next move out into an interface...

public interface ChessStrategy
{
public Move chooseMove( ChessGame game );
}

Of course, you would probably have two instances of ChessStrategy for any given game (one for each player). For a human player, the implementation might just block until the user clicks and drags a piece around on the board. For the CPU player, you could swap in MANY different implementations based upon the user's difficulty choice. For instance, the user could choose to play a very aggressive opponent, so you would swap in a strategy that was very aggressive. Or, if you're like me, you'd choose a very EASY opponent! :-) The key with the Strategy pattern is that you're decoupling some of the behavior from an object (and its state). You wouldn't want to have to create different subclasses of ChessGame for each type of opponent a user could choose. Also, suppose you have an object that you wish to behave differently over time. You don't want to have to swap in an entirely new object (maybe you've added many listeners to it or something), but you want its behavior to change. If you use the Strategy pattern, you could simply swap in a new Strategy object!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vasim Patel:
Isn't this the case of a simple OO polymorphism. what is the big deal abt having this as a named pattern? Isnt is so obvios? or am i missing something here?



You are probably missing that a pattern is more than just a code template. For example, the code of Proxy and Decorator look *exactly* the same - the difference is in the context they are used in, and the consequences following from using them.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vasim Patel:
Hi

Isn't this the case of a simple OO polymorphism. what is the big deal abt having this as a named pattern? Isnt is so obvios? or am i missing something here?


And In case of template pattern, instead of interface we have an abstract class called Strategy. rest remains the same.
Is this the only difference between Strategy and Template pattern?


Nothing, Patterns are defined on OO fundamentals anyways. Used named patterns makes it easy to figure out problem by looking at solution. BTW, when you write strategy pattern, you might want to avoide block statements like if and case otherwise there is no point using 'em.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic