Two Laptop Bag*
The moose likes OO, Patterns, UML and Refactoring and the fly likes strategy and template pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "strategy and template pattern" Watch "strategy and template pattern" New topic
Author

strategy and template pattern

Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
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

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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!


James Carman, President<br />Carman Consulting, Inc.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Udayan Patel
Ranch Hand

Joined: Oct 14, 2004
Posts: 94
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
 
subject: strategy and template pattern