Jump to content

Need help understanding how strategy patterns work


eldan88

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.