SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast silicate's Avatar
    Join Date
    Nov 2004
    Location
    Toronto
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strategy pattern

    Hey there,

    I was reading this example of implementing a Strategy Pattern that was posted in another thread.

    I was doing a working example of the pattern in a simple form. The example has a radio button for each of the flight options "fly", "can not fly". My question is what is the best way to get a unique identifier for the types of flight available to use in the form? If you have 4 flight behaviour types, and you want a unique identifier, would you add a method getFlightBehaviourID() that would return an Integer? That doesn't sound that good to me since I'd have to maintain the list of id's for each type of behaviour (swim, fly, etc.) and that could get labour intensive if I have hundreds of behaviours. Perhaps I could return the classname rather than a generic integer? Or, maybe I am not thinking about this in the right way at all.

    Thanks,

    Matthew.

  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ithink your missing the point of the pattern. Strategy encourages you to refactor parts of code which change depending on certain conditions. So in the example you give the author refactors the bird behaviours into separate classes because they are what changes depending on the type of bird in question. You would most likely not select a Strategy with a form.

    Imagine you have a database of birds. Each row has bird_id and bird_type. When loading a Bird object from the database you might use Strategy to load the correct behaviours based on the bird_type. The BirdBehaviourStrategy object would get passed as a parameter to the Bird object This way you have the same base code for all birds and specific behaviours for individual birds conditionally loaded at run time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •