Jump to content

Need help understanding how strategy patterns work


Go to solution Solved by ignace,

Recommended Posts

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!!!!

  • Solution

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.