Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added runtime decision to sorter #137

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2048,28 +2048,35 @@ And then we have our client that is going to use any strategy
```php
class Sorter
{
protected $sorter;
protected $sorterSmall;
protected $sorterBig;

public function __construct(SortStrategy $sorter)
public function __construct(SortStrategy $sorterSmall, SortStrategy $sorterBig)
{
$this->sorter = $sorter;
$this->sorterSmall = $sorterSmall;
$this->sorterBig = $sorterBig;
}

public function sort(array $dataset): array
{
return $this->sorter->sort($dataset);
if (count($dataset) > 5) {
return $this->sorterBig->sort($dataset);
} else {
return $this->sorterSmall->sort($dataset);
}
}
}
```
And it can be used as
```php
$dataset = [1, 5, 4, 3, 2, 8];
$smalldataset = [1, 3, 4, 2];
$bigdataset = [1, 4, 3, 2, 8, 10, 5, 6, 9, 7];

$sorter = new Sorter(new BubbleSortStrategy(), new QuickSortStrategy());

$sorter = new Sorter(new BubbleSortStrategy());
$sorter->sort($dataset); // Output : Sorting using bubble sort

$sorter = new Sorter(new QuickSortStrategy());
$sorter->sort($dataset); // Output : Sorting using quick sort
$sorter->sort($bigdataset); // Output : Sorting using quick sort
```

💢 State
Expand Down