kamranahmedse / design-patterns-for-humans

An ultra-simplified explanation to design patterns
45.45k stars 5.3k forks source link

Shouldn't the strategy pattern have condition checking in the example? #94

Closed archatas closed 6 years ago

archatas commented 7 years ago

I've read your writing about the patterns. Great work indeed!

I just have a question about the strategy pattern. In the description, you say, that it allows to choose an algorithm based on some conditions, but in the code example there is no condition checking. Is it incomplete, or is it not part of the pattern itself?

kamranahmedse commented 6 years ago

Hey, I meant you can decide and pick any strategy based on the application logic. It has nothing to do with the pattern.

Here you go with the conditional

$dataset = [1, 5, 4, 3, 2, 8];

$strategy = count($dataset > 100) ? new QuickSortStrategy() : new BubbleSortStrategy();

$sorter = new Sorter($strategy);
$sorter->sort($dataset);