This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Strategy Pattern Question confussion!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Strategy Pattern Question confussion!!!" Watch "Strategy Pattern Question confussion!!!" New topic
Author

Strategy Pattern Question confussion!!!

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Hi I am reading Head First Design Pattern book and learning what is Strategy pattern.

I have changed the example given in the book and applied it to a RaceCar. Here is my thinking...

To apply strategy pattern for an abstract RaceCar class first I need a reference in my RaceCar class to a Moveable interface. I draw it to explain my confussion easily for ranchers....

I need to give Move capability to my racecar with the help of an interface and the concrete classes should provide me this functionality. However my confussion is that are these concrete classes (? ones) be related to directly racecar. For example BMW move or AUDI move or they should be general and can have functions that take RaceCar instance in it?

Which solution is the best ? How can I design a racecar that its move cabability can change easily ?
My question is; Are these strategies can be more specific to the real objects(BMW Move)or they can be applied many objects (MOVEIT class) easily ?









[Thumbnail for e.GIF]


blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Hi Anut, I haven't read Head First Design Pattern book but given my understanding of strategy pattern and your diagram, I reckon your Moveable interface is the "strategy object". And instead of having a concrete class implementing Moveable interface directly, you want to have an abstract class in between. Right?

Approach 1


Approach 2


From your diagram, it looks like to me your are doing approach 1 above.

To make things simple, by removing the RaceCar abstract class would be a simple strategy pattern.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
 
jQuery in Action, 2nd edition
 
subject: Strategy Pattern Question confussion!!!
 
Similar Threads
Is it right way to implement the MVC?
What is the need for a pattern like factory Pattern?
Design Patterns...
Builder & Strategy Patterns?
Prefering interface over class inheritance