Created
April 18, 2012 09:54
-
-
Save Mezzle/2412492 to your computer and use it in GitHub Desktop.
Enterprise FizzBuzz
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 | |
interface INumber | |
{ | |
public function go(); | |
public function setNumber($i); | |
} | |
class FBNumber implements INumber | |
{ | |
private $value; | |
private $fizz; | |
private $buzz; | |
public function __construct($fizz, $buzz) | |
{ | |
$this->setFizz($fizz); | |
$this->setBuzz($buzz); | |
} | |
public function setNumber($i) | |
{ | |
if(is_int($i)) | |
{ | |
$this->value = $i; | |
} | |
} | |
private function setFizz($i) | |
{ | |
if(is_int($i)) | |
{ | |
$this->fizz = $i; | |
} | |
} | |
private function setBuzz($i) | |
{ | |
if(is_int($i)) | |
{ | |
$this->buzz = $i; | |
} | |
} | |
private function isFizz() | |
{ | |
return ($this->value % $this->fizz == 0); | |
} | |
private function isBuzz() | |
{ | |
return ($this->value % $this->buzz == 0); | |
} | |
private function isNeither() | |
{ | |
return (!$this->isBuzz() AND !$this->isFizz()); | |
} | |
private function isFizzBuzz() | |
{ | |
return ($this->isFizz() OR $this->isBuzz()); | |
} | |
private function fizz() | |
{ | |
if ($this->isFizz()) | |
{ | |
return "Fizz"; | |
} | |
} | |
private function buzz() | |
{ | |
if ($this->isBuzz()) | |
{ | |
return "Buzz"; | |
} | |
} | |
private function number() | |
{ | |
if ($this->isNeither()) | |
{ | |
return $this->value; | |
} | |
} | |
public function go() | |
{ | |
return $this->fizz() . $this->buzz() . $this->number(); | |
} | |
} | |
class FizzBuzz | |
{ | |
private $limit; | |
private $number_class; | |
private $numbers = array(); | |
function __construct(INumber $number_class, $limit = 100) | |
{ | |
$this->number_class = $number_class; | |
$this->limit = $limit; | |
$this->go(); | |
} | |
private function collectNumbers() | |
{ | |
for ($i=1; $i <= $this->limit; $i++) | |
{ | |
$n = clone($this->number_class); | |
$n->setNumber($i); | |
$this->numbers[$i] = $n->go(); | |
unset($n); | |
} | |
} | |
private function printNumbers() | |
{ | |
foreach($this->numbers as $number){ | |
print $number . "\n"; | |
} | |
} | |
public function go() | |
{ | |
$this->collectNumbers(); | |
$this->printNumbers(); | |
} | |
} | |
$fb = new FizzBuzz(new FBNumber(3,5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment