Last active
October 27, 2015 21:02
-
-
Save gmansilla/fd48b656c5dba76dd4e7 to your computer and use it in GitHub Desktop.
forecast
This file contains hidden or 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
| <?php | |
| // implementers are responsible for doing the necessary math, store the data in whatever structure it requires | |
| interface IForecaster | |
| { | |
| public function fetchForecastResult(); | |
| } | |
| // only encapsulates the result of a forecast, DOES NOT DO ANY CALCULATIONS OR LOGIC | |
| interface IForecastResult | |
| { | |
| public function getSpec(); | |
| public function isScalar(); | |
| public function isArray(); | |
| public function getResult(); // returns array or scalar value | |
| } | |
| abstract class ForecastResultBase implements IForecastResult | |
| { | |
| protected $spec; | |
| protected $result; | |
| public function __construct(Spec $spec, array $result = []) | |
| { | |
| $this->spec = $spec; | |
| $this->result = $result; | |
| } | |
| public function getSpec() | |
| { | |
| return $this->spec; | |
| } | |
| public function getResult() | |
| { | |
| return $this->result; | |
| } | |
| public abstract function isScalar(); | |
| public abstract function isArray(); | |
| } | |
| class ToiletForecastResult extends ForecastResultBase | |
| { | |
| } | |
| class StandardForecastResult extends ForecastResultBase | |
| { | |
| } | |
| abstract class FixtureTypeForecasterBase implements IForecaster | |
| { | |
| public function __construct(Spec $spec, IForecastResult $forecastResult = null) | |
| { | |
| $this->spec = $spec; | |
| $this->forecastResult = $forecastResult; | |
| } | |
| public abstract function fetchForecastResult(); | |
| } | |
| class ToiletForecaster extends FixtureTypeForecasterBase | |
| { | |
| private $result; | |
| public function __constructor(Spec $spec, IForecastResult $forecastResult = null) | |
| { | |
| $concreteForecaster = $this->getConcreteForecaster(); | |
| $this->result = $concreteForecast->getQuantities(); // | |
| } | |
| public function fetchForecastResult() | |
| { | |
| new ToiletForecastResult($this->spec, $this->result); | |
| } | |
| protected function getConcreteForecaster() | |
| { | |
| if ($this->spec->something() == 'commercial') { | |
| return new CommercialUseToiletForecaster($this->spec); | |
| } | |
| return new ResidentialUseToiletForecaster($this->spec); | |
| } | |
| } | |
| // pretend this is for grab bar | |
| class StandardFixtureTypeForecaster extends FixtureTypeForecasterBase | |
| { | |
| public function fetchForecastResult() | |
| { | |
| $toiletsWithoutUrinals = $this->forecastResult->getResult()['something']; // you need to know the index in advance | |
| new StandardForecastResult($this->spec, $something); | |
| } | |
| } | |
| $spec = new Spec(); | |
| $toiletForecaster = Factory::build($spec, FixtureType::TOILET); | |
| $toiletForecastResult = $toiletForecaster->fetchForecastResult(); | |
| $grabBarForecaster = Factory::build(FixtureType::GRAB_BAR, $toiletForecastResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment