Created
September 3, 2020 02:09
-
-
Save mcsee/eae5a5035af62088a004f24ed6074986 to your computer and use it in GitHub Desktop.
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
<? | |
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