php设计模式-简单工厂模式
<?php
header('Content-Type:text/html;charset=utf-8');
// 简单工厂模式(静态工厂方法模式)
interface people{
public function say();
}
// 继承people类
class man implements people{
public function say(){
echo "man";
}
}
class woman implements people{
public function say(){
echo "woman";
}
}
// 工厂类
class SimpleFactory{
// 简单工厂里的静态方法-用于创建对象
static function createMan(){
return new man();
}
static function createWoman(){
return new woman();
}
}
// 具体调用
$man = SimpleFactory::createMan();
$man->say();
$woman = SimpleFactory::createWoman();
$woman->say();
本文标题:php设计模式-简单工厂模式
本文链接:https://www.haomeiwen.com/subject/chnavftx.html
网友评论