interface Product
{
public function getName();
}
class ConcreteProductA implements Product
{
public function getName()
{
return 'Product A';
}
}
class ConcreteProductB implements Product
{
public function getName()
{
return 'Product B';
}
}
class Factory
{
public static function createProduct($type)
{
if ($type === 'A') {
return new ConcreteProductA();
} elseif ($type === 'B') {
return new ConcreteProductB();
}
throw new \InvalidArgumentException('Unknown product type given');
}
}
$productA = Factory::createProduct('A');
$productB = Factory::createProduct('B');
01-04
454