php设计模式-工厂方法模式
2018-08-07 本文已影响0人
destiny96
<?php
header('Content-Type:text/html;charset=utf-8');
interface people{
public function say();
}
// man类 继承people
class man implements people{
public function say(){
echo "man-1";
}
}
// woman类 继承people
class woman implements people{
public function say(){
echo "woman-1";
}
}
// 创建对象类
// 将对象的创建抽象成了一个接口
interface createPeople{
public function create();
}
// 用于创建man对象的工厂类 继承createpeople
class FactoryMan implements createPeople{
public function create(){
return new man();
}
}
// 用于创建woman对象的工厂类 继承createpeople
class FactoryWoman implements createPeople{
public function create(){
return new woman();
}
}
// 具体操作类
class Client{
public function test(){
$factory = new FactoryMan();
$man = $factory->create();
$man->say();
$factory = new FactoryWoman();
$woman = $factory->create();
$woman->say();
}
}
$result = new Client;
$result->test();