Last active
September 13, 2019 17:34
-
-
Save JasonTheAdams/f56c9490227aa28ac6597e98586fa8a3 to your computer and use it in GitHub Desktop.
Example of the Abstract Factory Design Pattern
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 | |
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