aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Stratergy Design 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 "Stratergy Design Pattern?" Watch "Stratergy Design Pattern?" New topic
Author

Stratergy Design Pattern?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

I'm done with the first chapter of he HFDS book and now I know what a stratergy design pattern means. Can anyone help me with a real time example of what it actually means and where such a pattern would be useful??

Thanks in advance!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I guess you mean real *world* examples - real time is something very different...

Take a look at http://wiki.cs.uiuc.edu/PatternStories/StrategyPattern


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
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

What did you actually mean by saying real time is somewhat different?? Could you please tell me!
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
google define:"real time"

google define:"real world"
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"real time" for "real life" has been pretty common lately. Maybe one of those language things the rest of us have to get used to like "doubt"? Anyhow, let's guess it really mean "real life" example.

Strategy is something I use all the time. In fact I'm probably overly fond of it and may use it too often, but that's another issue. My work system can search for products owned by a customer using customer name, contract number, etc. The product information is housed in different systems with different calling protocols. A few years ago the code looked like this:

Every time we added a new product - fairly often - we had to open up this code, change it, test it, deploy it, all that expensive and risky stuff. Now the code looks like:

We map product types to strategy implementation classes in configuration. We can add new products without touching this code, just by adding new search strategies.

Does that show some value for Strategy?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Stan,

I'm getting what you are trying to say. Thanks for the explanation!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jothi Shankar Kumar Sankararaj:
What did you actually mean by saying real time is somewhat different?? Could you please tell me!


Real time software systems are systems that make some specific guarantees regarding response times. That's important for things like live video processing for example. It's not possible with vanilla Java because of the garbage collector. See also http://java.sun.com/javase/technologies/realtime.jsp
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Ilja, thanks for the help!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Guys,

On what basis do we select a particular pattern. I'm done with the second chapter of HFDS and I want o know the stratergy used to decide on using a particular pattern for the job? Can anyone help me on this?? Thanks in advance!
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
http://en.wikipedia.org/wiki/Strategy_pattern
The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.


I've used strategy pattern even when there's no need to swap algorithms during real time. I use it when heavy development or refinement is expected on the algorithm. This allows the stable parts of the software work through a stable interface.

For instance, parsing some XML string into a custom object. I expect the xml format to evolve over time so I apply the strategy pattern to the parsing logic which returns the custom object. In this way, I can swap in stubs during unit tests, go from DOM to SAX API or extend the algorithm safely.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What happens when you have a certain familiarity with the patterns is you see them start to emerge from your own code. Then you can recognize the pattern, perhaps review a reference book to see if there is something you should take into account, and rename classes and methods to fit the pattern description for the benefit of future readers. Or you may recognize a match with a pattern's forces and indicators right in a problem description.

There is a risk in jumping into solution patterns too early in design. You might have a partial fit or a poor fit and go the wrong direction for a while before you see it. That's why I suggested my fondness for Strategy could be a problem. I smack myself around regularly to stay alert for wrong turns.

BTW: Strategy is also a common way to replace a long if-else-if chain or even a switch/case statement with a stable mechanism that can survive extension with little or no change.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Thanks Stan! Your argument seems meaningful to me!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stratergy Design Pattern?