Created
March 14, 2017 12:32
-
-
Save ArtyomLazyan/f0205e3d0ca2a509396dcad260850312 to your computer and use it in GitHub Desktop.
Strategy Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************* Strategy Pattern ************************/ | |
<?php | |
interface SortStrategy | |
{ | |
public function sort(); | |
} | |
class QuickSort | |
{ | |
private $data; | |
function __construct(Array $data) | |
{ | |
$this->data = $data; | |
} | |
public function sort() | |
{ | |
// sort algorithm1 | |
} | |
} | |
class MergeSort | |
{ | |
private $data; | |
function __construct(Array $data) | |
{ | |
$this->data = $data; | |
} | |
public function sort() | |
{ | |
// sort alghoritm2 | |
} | |
} | |
function arraySort(&$data) | |
{ | |
if (count($data) > 20) | |
$tempData = new QuickSort($data); | |
else | |
$tempData = new MergeSort($data); | |
$tempData->sort(); | |
} | |
$data = array(5, 2, 24, 98, 55, 245, 24, 32, 55, 22); | |
$len = count($data); | |
arraySort($data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment