-
-
Save anonymous/f4b3e19af158f0cde10f to your computer and use it in GitHub Desktop.
Simple Factory
This file contains 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 namespace Pattern\SimpleFactory; | |
interface Car { | |
} |
This file contains 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 namespace Pattern\SimpleFactory; | |
use InvalidArgumentException; | |
class CarFactory { | |
private $cars = [ | |
'equinox' => 'Pattern\SimpleFactory\Car\Equinox', | |
'evos' => 'Pattern\SimpleFactory\Car\Evos', | |
'galaxy' => 'Pattern\SimpleFactory\Car\Galaxy', | |
'miray' => 'Pattern\SimpleFactory\Car\Miray', | |
]; | |
public function make($name) | |
{ | |
if (! isset($this->cars[$name])) | |
{ | |
throw new InvalidArgumentException('You tried to make a nonexistent car'); | |
} | |
return new $this->cars[$name]; | |
} | |
} |
This file contains 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 namespace Pattern\SimpleFactory; | |
class CarStore { | |
private $factory; | |
function __construct(CarFactory $factory) | |
{ | |
$this->factory = $factory; | |
} | |
public function order($name) | |
{ | |
return $this->factory->make($name); | |
} | |
} |
This file contains 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 namespace Pattern\SimpleFactory\Car; | |
use Pattern\SimpleFactory\Car; | |
class Equinox implements Car { | |
} |
This file contains 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 namespace Pattern\SimpleFactory\Car; | |
use Pattern\SimpleFactory\Car; | |
class Evos implements Car { | |
} |
This file contains 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 namespace Pattern\SimpleFactory\Car; | |
use Pattern\SimpleFactory\Car; | |
class Galaxy implements Car { | |
} |
This file contains 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 namespace Pattern\SimpleFactory\Car; | |
use Pattern\SimpleFactory\Car; | |
class Miray implements Car { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment