Design Patterns - Strategy
Problem: London Marathon Race Participants Reporting Given a list of Participants reports are required, participants list, by gender, by age range, etc. Implementing Strategy pattern was chosen as a first iteration of problem solving.
Participant Entity
// Array of "Participants" for simplicity $participants = Array( array('name' => 'jose', 'age' => 41, 'gender' => 'M', 'paid' => true), array('name' => 'maria', 'age' => 18, 'gender' => 'F', 'paid' => false), array('name' => 'manuel', 'age' => 52, 'gender' => 'M', 'paid' => false), array('name' => 'antonio', 'age' => 20, 'gender' => 'M', 'paid' => true), array('name' => 'joana', 'age' => 41, 'gender' => 'F', 'paid' => true), );Defining our report "black box
// Rules are the conditions for a particular report
class Report {
private $participants;
public function __construct($participants) {
$this->participants = $participants;
}
/**
* @param Rule[] $rules
* @return array
*/
public function getList($rules) {
// our report
$outputList = array();
// run thu all participants
foreach($this->participants as $participant) {
// check if participant is valid with all rules
$valid = true;
foreach($rules as $rule) {
// is participant valid with this rule
$valid &= $rule->isEligible($participant);
if (!$valid) {
break;
}
}
// if true, all rules are valid for this participant, add to output list
if ($valid) {
$outputList[] = $participant;
}
}
return $outputList;
}
};
Rules
Now defining rules as required by business // Rule definition, a Strategy
Interface Rule {
// all rules take a participant and "say" if he is eligible
public function isEligible($participant);
}
// implementing Gender
class Gender implements Rule {
private $gender;
// the context for this report
public function __construct($gender) {
$this->gender = $gender;
}
public function isEligible($participant) {
// is the participant the same gender as required by report?
return $participant['gender'] === $this->gender;
}
}
// implementing Age, takes a range
class Age implements Rule {
private $min;
private $max;
// the context for this report is for a age range
public function __construct($min, $max) {
$this->max = $max;
$this->min = $min;
}
// check if participant is in report range
public function isEligible($participant) {
return ($participant['age'] >= $this->min && $participant['age'] <= $this->max);
}
}
Using the report
// defining rules for Male Senior participants
$cat1 = array(
new Gender('M'),
new Age(18, 39)
);
// creating a report context
$report = new Report($participants);
// getting the report for Category Male Senior, yeah i know var_dump...
var_dump($report->getList($cat1));
// now, lets get Female Veteran
$cat2 = array(
new Gender('F'),
new Age(40, 49)
);
// reusing the same participant list
var_dump($report->getList($cat2));
This will output one Participant in each category:
array(1) {
[0]=>
array(4) {
["name"]=>
string(7) "antonio"
["age"]=>
int(20)
["gender"]=>
string(1) "M"
["paid"]=>
bool(true)
}
}
array(1) {
[0]=>
array(4) {
["name"]=>
string(5) "joana"
["age"]=>
int(41)
["gender"]=>
string(1) "F"
["paid"]=>
bool(true)
}
}
Implementing new rules
Now that everything was "deployed" business asked for a new report but with the flag paid/not paid, so, all you have to do is implement a new Rule (Strategy) class Paid implements Rule {
private $paid;
// setting the report context
public function __construct($paid) {
$this->paid = $paid;
}
public function isEligible($participant) {
// check if participant is in same context as report
return $participant['paid'] === $this->paid;
}
}
This change doesn't break existing code, and to use this new rule:
// all Male Participants that Paid
$cat4 = Array(
new Gender('M'),
new Paid(true)
);
var_dump($report->getList($cat4));
And this will output 2 Participants in Male and Paid;
array(2) {
[0]=>
array(4) {
["name"]=>
string(4) "jose"
["age"]=>
int(41)
["gender"]=>
string(1) "M"
["paid"]=>
bool(true)
}
[1]=>
array(4) {
["name"]=>
string(7) "antonio"
["age"]=>
int(20)
["gender"]=>
string(1) "M"
["paid"]=>
bool(true)
}
}