/**
* Class Creator 工厂类
*/
abstract class Creator{
protected abstract function factoryMethod();
public function startFactory(){
return $this->factoryMethod();
}
}
/**
* Class FruitsFactory 水果工厂
*/
class FruitsFactory extends Creator
{
protected function factoryMethod(){
$obj = new Apple();
return $obj->getProperties();
}
}
/**
* Class Pen 笔工厂
*/
class PenFactory extends Creator{
protected function factoryMethod()
{
$obj = new Pencil();
return $obj->getProperties();
}
}
/**
* Class Apple 苹果类
*/
class Apple{
public function getProperties(){
echo 'making Apple <br/>';
}
}
/**
* Class Pencil 铅笔类
*/
class Pencil{
public function getProperties(){
echo 'making Pencil <br/>';
}
}
//示例
$a = new FruitsFactory();
$b = new PenFactory();
$a->startFactory();
$b->startFactory();
php 设计模式之工厂方法模式
最新推荐文章于 2024-10-05 08:45:32 发布