• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stratergy Design Pattern?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you mean real *world* examples - real time is something very different...

Take a look at http://wiki.cs.uiuc.edu/PatternStories/StrategyPattern
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you actually mean by saying real time is somewhat different?? Could you please tell me!
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

I'm getting what you are trying to say. Thanks for the explanation!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja, thanks for the help!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan! Your argument seems meaningful to me!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic