什么是簡單工廠模式
簡單工廠模式又被稱為靜態(tài)工廠方法模式(Static Factory Method)
具體就是通過這個工廠類的方法來創(chuàng)建對象
```
<?php
abstract class Product
{
abstract function play();
}
class A extends Product
{
public function play()
{
echo 'I play CS:GO';
}
}
class B extends Product
{
public function play()
{
echo 'I am playing LOL';
}
}
class SimpleFactory
{
static function create($name)
{
switch($name) {
case 'cs':
return new A();
break;
case 'lol':
return new B();
break;
default:
break;
}
}
}
$obj_1 = SimpleFactory::create('cs');
$obj_1->play(); // I play CS:GO
$obj_2 = SimpleFactory::create('lol');
$obj_2->play(); // I am playing LOL