This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
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
"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
I'm getting what you are trying to say. Thanks for the explanation!
Joined: Jul 11, 2001
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
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!
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.
Joined: Jan 29, 2003
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.