Skip to content

Instantly share code, notes, and snippets.

@Mezzle
Created April 18, 2012 09:54
Show Gist options
  • Save Mezzle/2412492 to your computer and use it in GitHub Desktop.
Save Mezzle/2412492 to your computer and use it in GitHub Desktop.
Enterprise FizzBuzz
<?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