eldan88 Posted September 30, 2013 Share Posted September 30, 2013 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!!!! Quote Link to comment https://forums.phpfreaks.com/topic/282585-need-help-understanding-how-strategy-patterns-work/ Share on other sites More sharing options...
Solution ignace Posted October 1, 2013 Solution Share Posted October 1, 2013 (edited) 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 October 1, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/282585-need-help-understanding-how-strategy-patterns-work/#findComment-1451985 Share on other sites More sharing options...
eldan88 Posted October 1, 2013 Author Share Posted October 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282585-need-help-understanding-how-strategy-patterns-work/#findComment-1452117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.