Skip to content

Instantly share code, notes, and snippets.

@JasonTheAdams
Last active September 13, 2019 17:34
Show Gist options
  • Save JasonTheAdams/f56c9490227aa28ac6597e98586fa8a3 to your computer and use it in GitHub Desktop.
Save JasonTheAdams/f56c9490227aa28ac6597e98586fa8a3 to your computer and use it in GitHub Desktop.
Example of the Abstract Factory Design Pattern
<?php
class ChairFactory
{
public function createChair(bool $cheap, bool $fancy): ChairInterface
{
if ($cheap && $fancy) {
return new ModernChair();
} elseif ($fancy) {
return new VictorianRockingChair();
} elseif ($cheap) {
return new SimpleChair();
} else {
throw new Exception('No chair matches that criteria');
}
}
}
interface ChairInterface
{
public function getPrice();
}
class SimpleChair implements ChairInterface
{
public function getPrice()
{
return 79.99;
}
}
class ModernChair implements ChairInterface
{
public function getPrice()
{
return 149.99;
}
public function swivel()
{
// See the world around you
}
}
class VictorianRockingChair implements ChairInterface
{
public function getPrice()
{
return 199.99;
}
public function rockBackAndForth()
{
// rock out
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment