Jump to content


Photo

Need help understanding how strategy patterns work


Best Answer ignace, 01 October 2013 - 12:45 AM

By interchangeable they mean you can swap one for the other, as in setStrategy($strategy), now the object who has that method has his strategy changed, for example if you have a class that displays the filesystem in an hierarchy, a strategy could be to sort it by name (class SortByNameStrategy) or sorted by folders-first (class SortByFolderFirstStrategy).

You can change the display by changing the strategy on the filesystem object. Adapter and Strategy are quite alike from a design POV where one is about making one thing work with another, the other is about selecting the proper algorithm/behaviour. Go to the full post


  • Please log in to reply
2 replies to this topic

#1 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 488 posts
  • LocationNew York

Posted 30 September 2013 - 06:27 PM

Hey guys. I have been trying to comprehend  how strategy patterns work.

From what I understand, they are a family of algorithms that are encapsulated in an object. (sounds self-explanatory)

But what I don't get, is what they mean by making the objects interchangeable?

 

I have been doing a lot of googling but can't find a good easy to understand example.

Does anyone have any references or examples, that you can show me?

 

Thanks a lot!!!!



#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,249 posts
  • LocationBelgium

Posted 01 October 2013 - 12:45 AM   Best Answer

By interchangeable they mean you can swap one for the other, as in setStrategy($strategy), now the object who has that method has his strategy changed, for example if you have a class that displays the filesystem in an hierarchy, a strategy could be to sort it by name (class SortByNameStrategy) or sorted by folders-first (class SortByFolderFirstStrategy).

You can change the display by changing the strategy on the filesystem object. Adapter and Strategy are quite alike from a design POV where one is about making one thing work with another, the other is about selecting the proper algorithm/behaviour.

Edited by ignace, 01 October 2013 - 12:49 AM.


#3 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 488 posts
  • LocationNew York

Posted 01 October 2013 - 05:46 PM

Thanks for the explanation ignace. I re-read your explanation and did some further reading and finally understood how it works. It was that AHA! moment.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com