1.7. 단순팩토리 (Simple Factory)¶
1.7.1. 사용 목적¶
ConcreteFactory(코드에 언급된)는 단순팩토리입니다.
단순팩토리패턴은 정적이지 않기 때문에 정적팩토리패턴(Static Factory)과는 다릅니다. 참고: 정적(static) => 전역(global) => 좋지않음(evil)!
그러므로, 서로 다른 패러매터를 가진 여러개의 팩토리를 가질 수 있고, 이것의 상속된 클래스를 만들 수 있으며, 이것의 목업(mock-up)을 생성할 수 있습니다.
1.7.2. UML 다이어그램¶
1.7.3. 코드¶
코드는 또한 GitHub 에서 볼 수 있습니다.
ConcreteFactory.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php
namespace DesignPatterns\Creational\SimpleFactory;
/**
* class ConcreteFactory
*/
class ConcreteFactory
{
/**
* @var array
*/
protected $typeList;
/**
* You can imagine to inject your own type list or merge with
* the default ones...
*/
public function __construct()
{
$this->typeList = array(
'bicycle' => __NAMESPACE__ . '\Bicycle',
'other' => __NAMESPACE__ . '\Scooter'
);
}
/**
* Creates a vehicle
*
* @param string $type a known type key
*
* @return VehicleInterface a new instance of VehicleInterface
* @throws \InvalidArgumentException
*/
public function createVehicle($type)
{
if (!array_key_exists($type, $this->typeList)) {
throw new \InvalidArgumentException("$type is not valid vehicle");
}
$className = $this->typeList[$type];
return new $className();
}
}
|
VehicleInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
namespace DesignPatterns\Creational\SimpleFactory;
/**
* VehicleInterface is a contract for a vehicle
*/
interface VehicleInterface
{
/**
* @param mixed $destination
*
* @return mixed
*/
public function driveTo($destination);
}
|
Bicycle.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
namespace DesignPatterns\Creational\SimpleFactory;
/**
* Bicycle is a bicycle
*/
class Bicycle implements VehicleInterface
{
/**
* @param mixed $destination
*
* @return mixed|void
*/
public function driveTo($destination)
{
}
}
|
Scooter.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
namespace DesignPatterns\Creational\SimpleFactory;
/**
* Scooter is a Scooter
*/
class Scooter implements VehicleInterface
{
/**
* @param mixed $destination
*/
public function driveTo($destination)
{
}
}
|
1.7.4. 테스트¶
Tests/SimpleFactoryTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php
namespace DesignPatterns\Creational\SimpleFactory\Tests;
use DesignPatterns\Creational\SimpleFactory\ConcreteFactory;
/**
* SimpleFactoryTest tests the Simple Factory pattern
*/
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
{
protected $factory;
protected function setUp()
{
$this->factory = new ConcreteFactory();
}
public function getType()
{
return array(
array('bicycle'),
array('other')
);
}
/**
* @dataProvider getType
*/
public function testCreation($type)
{
$obj = $this->factory->createVehicle($type);
$this->assertInstanceOf('DesignPatterns\Creational\SimpleFactory\VehicleInterface', $obj);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBadType()
{
$this->factory->createVehicle('car');
}
}
|