-
-
Save anonymous/ca1a7a6552c6125b7032 to your computer and use it in GitHub Desktop.
Factory Method
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\FactoryMethod; | |
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\FactoryMethod; | |
use InvalidArgumentException; | |
abstract class CarFactory { | |
private $cars = []; | |
function __construct(array $cars) | |
{ | |
$this->cars = $cars; | |
} | |
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\FactoryMethod; | |
class CarStore { | |
private $factory; | |
function __construct(CarFactory $factory) | |
{ | |
$this->factory = $factory; | |
} | |
public function order($name) | |
{ | |
$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\FactoryMethod\Factory; | |
use Pattern\FactoryMethod\CarFactory; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class Chevrolet extends CarFactory { | |
private $cars = [ | |
'equinox' => 'Pattern\SimpleFactory\Car\Equinox', | |
'miray' => 'Pattern\SimpleFactory\Car\Miray', | |
]; | |
function __construct() | |
{ | |
parent::__construct($this->cars); | |
} | |
} |
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\FactoryMethod\Car; | |
use Pattern\FactoryMethod\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\FactoryMethod\Car; | |
use Pattern\FactoryMethod\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\FactoryMethod\Factory; | |
use Pattern\FactoryMethod\CarFactory; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class Ford extends CarFactory { | |
private $cars = [ | |
'evos' => 'Pattern\SimpleFactory\Car\Evos', | |
'galaxy' => 'Pattern\SimpleFactory\Car\Galaxy', | |
]; | |
function __construct() | |
{ | |
parent::__construct($this->cars); | |
} | |
} |
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\FactoryMethod\Car; | |
use Pattern\FactoryMethod\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\FactoryMethod\Car; | |
use Pattern\FactoryMethod\Car; | |
class Miray implements Car { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment