This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strategy and template pattern

 
Vasim Patel
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Udayan Patel
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic