Skip to content

Instantly share code, notes, and snippets.

@mcsee
Created September 3, 2020 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsee/eae5a5035af62088a004f24ed6074986 to your computer and use it in GitHub Desktop.
Save mcsee/eae5a5035af62088a004f24ed6074986 to your computer and use it in GitHub Desktop.
<?
Interface Visitable {
public function accept($aVisitor);
}
final class Date implements Visitable {
public function accept($aVisitor) {
$aVisitor->visitDate($this);
}
}
final class DateNotPresent implements Visitable {
public function accept($aVisitor) {
$aVisitor->visitDateNotPresent($this);
}
}
final class AverageCalculator {
private $count = 0;
private $ageSum = 0;
public function visitDate($aDate) {
$this->count++;
$this->ageSum += today() - $aDate;
}
public function visitDateNotPresent($aDate) {
}
public function average() {
if ($this->count == 0)
return 0;
else
return $this->ageSum / $this->count;
}
}
function averagePatientsAge($patients) {
$calculator = new AverageCalculator();
foreach ($patients as $patient)
$patient->birthDate()->accept($calculator);
return $calculator->average();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment