-
-
Save anonymous/f0c4442175c23b9fe101 to your computer and use it in GitHub Desktop.
Abstract 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\AbstractFactory; | |
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\AbstractFactory; | |
use InvalidArgumentException; | |
abstract class CarFactory { | |
private $sports = []; | |
private $family = []; | |
function __construct(array $sports, array $family) | |
{ | |
$this->sports = $sports; | |
$this->family = $family; | |
} | |
public function makeSportsCar($name) | |
{ | |
return $this->makeCar($this->sports, $name); | |
} | |
public function makeFamilyCar($name) | |
{ | |
return $this->makeCar($this->family, $name); | |
} | |
private function makeCar(array $cars, $name) | |
{ | |
if (! isset($cars[$name])) | |
{ | |
throw new InvalidArgumentException('You tried to make a nonexistent car'); | |
} | |
return new $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\AbstractFactory; | |
class CarStore { | |
private $factory; | |
function __construct(CarFactory $factory) | |
{ | |
$this->factory = $factory; | |
} | |
public function orderSportsCar($name) | |
{ | |
return $this->factory->makeSportsCar($name); | |
} | |
public function orderFamilyCar($name) | |
{ | |
return $this->factory->makeFamilyCar($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\AbstractFactory\Factory; | |
use Pattern\AbstractFactory\CarFactory; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class Chevrolet extends CarFactory { | |
private $sports = [ | |
'miray' => 'Pattern\AbstractFactory\Car\Sport\Miray' | |
]; | |
private $family = [ | |
'equinox' => 'Pattern\AbstractFactory\Car\Family\Equinox' | |
]; | |
function __construct() | |
{ | |
parent::__construct($this->sports, $this->family); | |
} | |
} |
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\AbstractFactory\Car\Family; | |
use Pattern\AbstractFactory\Car\Family; | |
class Equinox implements Family { | |
} |
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\AbstractFactory\Car\Sport; | |
use Pattern\AbstractFactory\Car\Sport; | |
class Evos implements Sport { | |
} |
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\AbstractFactory\Car; | |
use Pattern\AbstractFactory\Car; | |
interface Family extends 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\AbstractFactory\Factory; | |
use Pattern\AbstractFactory\CarFactory; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class Ford extends CarFactory { | |
private $sports = [ | |
'evos' => 'Pattern\AbstractFactory\Car\Sport\Evos' | |
]; | |
private $family = [ | |
'galaxy' => 'Pattern\AbstractFactory\Car\Family\Galaxy' | |
]; | |
function __construct() | |
{ | |
parent::__construct($this->sports, $this->family); | |
} | |
} |
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\AbstractFactory\Car\Family; | |
use Pattern\AbstractFactory\Car\Family; | |
class Galaxy implements Family { | |
} |
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\AbstractFactory\Car\Sport; | |
use Pattern\AbstractFactory\Car\Sport; | |
class Miray implements Sport { | |
} |
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\AbstractFactory\Car; | |
use Pattern\AbstractFactory\Car; | |
interface Sport extends Car { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment