Note that the apply method is marked as abstract. That means that we'll need to determine what method to call by considering the child class of which the object is an instance. In this case, the child class is a Decorator with $name == 'Fizz' and $number == 3. So let's consider the apply method of the Decorator.
We check whether the $number argument is divisible by the value of the $this->number member variable. 5 is not divisible by 3 so we take the else branch of the conditional. We pass forward the $number argument to the next rule along with the $matched boolean indicating whether any previous rule (which is still False).
The next rule is a Decorator with $name == 'Buzz' and $number == 5. 5 is divisible by 5 so we take the first branch of the conditional. Again we pass forward the $number argument to the next rule, but this time, because we've found a match, we pass True as the boolean argument.
The next rule is a Decorator with $name == 'Bazz' and $number == 7. 5 is not divisible by 7 so again we take the else branch of the conditional. We pass forward the $number argument to the next rule along with the $matched boolean indicating whether any previous rule (which is now True).
The last rule is a Bottom. This class serves as a base case for this sort of object-oriented recursion. Let's consider it's apply method.
If no previous rule has matched the argument $number (as indicated by the $matched argument), we need to print the value of $number to the console. In either case, we print an end of line character to console to complete an iteration of the fizzbuzz loop.
Thanks for the explanation. Very impressive. I checked your profile to get your Twitter account but it seems you are not on Twitter yet. I got to follow you so we can engage in discussions in the future.
Sure Thing!
Let's consider a call to
FizzBuzzBazzRuleto see what that boolean flag is doing.We begin by calling to the public facing
evalmethod defined in theRuleabstract class.This method forwards the argument to the protected
applymethod, but also passes a second boolean parameterFalse.This boolean parameter is used to indicate whether any previous rules have successfully matched against the
$numberargument.Because
evalis called by the client, no previous rules have run and consequently, the$numberhas not yet matched against any previous rule.Note that the
applymethod is marked asabstract. That means that we'll need to determine what method to call by considering the child class of which the object is an instance. In this case, the child class is aDecoratorwith$name == 'Fizz'and$number == 3. So let's consider the apply method of theDecorator.We check whether the
$numberargument is divisible by the value of the$this->numbermember variable.5is not divisible by3so we take theelsebranch of the conditional. We pass forward the$numberargument to the next rule along with the$matchedboolean indicating whether any previous rule (which is stillFalse).The next rule is a
Decoratorwith$name == 'Buzz'and$number == 5.5is divisible by5so we take the first branch of the conditional. Again we pass forward the$numberargument to the next rule, but this time, because we've found a match, we passTrueas the boolean argument.The next rule is a
Decoratorwith$name == 'Bazz'and$number == 7.5is not divisible by7so again we take theelsebranch of the conditional. We pass forward the$numberargument to the next rule along with the$matchedboolean indicating whether any previous rule (which is nowTrue).The last rule is a
Bottom. This class serves as a base case for this sort of object-oriented recursion. Let's consider it'sapplymethod.If no previous rule has matched the argument
$number(as indicated by the$matchedargument), we need to print the value of$numberto the console. In either case, we print an end of line character to console to complete an iteration of thefizzbuzzloop.Thanks for the explanation. Very impressive. I checked your profile to get your Twitter account but it seems you are not on Twitter yet. I got to follow you so we can engage in discussions in the future.
twitter.com/RakishRhenoplos
Happy to chat anytime. =)