• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strategy Pattern Question confussion!!!

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?






e.GIF
[Thumbnail for e.GIF]
Strategy
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic