抽象工厂模式是对工厂模式的抽象,通俗来说,就是把工厂模式的结构分离出来成为能独立运行的个体。
还是拿工厂模式中的例子来说明:
现在有一个汽车工厂,它生产小汽车和巴士车,小汽车和巴士车都是由引擎、车身和轮子组成的。
在工厂模式中,我们把小汽车和巴士车作为汽车族群中的两个类别,生产引擎、车身和轮子为生产汽车的固定结构,如下图所示:
在抽象工厂模式中,把生产引擎、车身和轮子分别抽象出来,如下图所示:

实际部署为:
//生产引擎的标准
interface engineNorms{
function engine();
}
class carEngine implements engineNorms{
public function engine(){
return '汽车引擎';
}
}
class busEngine implements engineNorms{
public function engine(){
return '巴士车引擎';
}
}
//生产车身的标准
interface bodyNorms{
function body();
}
class carBody implements bodyNorms{
public function body(){
return '汽车车身';
}
}
class busBody implements bodyNorms{
public function body(){
return '巴士车车身';
}
}
//生产车轮的标准
interface whellNorms{
function whell();
}
class carWhell implements whellNorms{
public function whell(){
return '汽车轮子';
}
}
class busWhell implements whellNorms{
public function whell(){
return '巴士车轮子';
}
}再继续对工厂进行抽象,抽象出汽车工厂和巴士车工厂,并且让各工厂与各组件相关联,如图:
立即学习“PHP免费学习笔记(深入)”;
实际部署为:
实际部署为:
//生产引擎的标准
interface engineNorms{
function engine();
}
class carEngine implements engineNorms{
public function engine(){
return '汽车引擎';
}
}
class busEngine implements engineNorms{
public function engine(){
return '巴士车引擎';
}
}
//生产车身的标准
interface bodyNorms{
function body();
}
class carBody implements bodyNorms{
public function body(){
return '汽车车身';
}
}
class busBody implements bodyNorms{
public function body(){
return '巴士车车身';
}
}
//生产车轮的标准
interface whellNorms{
function whell();
}
class carWhell implements whellNorms{
public function whell(){
return '汽车轮子';
}
}
class busWhell implements whellNorms{
public function whell(){
return '巴士车轮子';
}
}<br><br>
//工厂标准<br>
interface factory
{<br>
static public function getInstance($type);
<br>
}<br><br>
//汽车工厂<br>class carFactory implements factory{<br>
<br>
static public function getInstance($type){<br>
$instance='';<br>
switch($type){<br>
case 'engine':<br>
$instance=new carEngine();
<br>
break;<br>
case 'body':<br>
$instance=new carBody();<br>
break;<br>
case 'whell':<br>
$instance=new carWhell();<br>
break;
<br>
default:<br>
throw new Exception('汽车工厂无法生产这种产品');<br>
}<br>
return $instance;<br>
}<br>
<br>}<br><br>
//巴士车工厂<br>
class busFactory implements factory{<br>
<br>
static public function getInstance($type){<br>
$instance='';<br>
switch($type){<br>
case 'engine':<br>
$instance=new busEngine();<br>
break;<br>
case 'body':<br>
$instance=new busBody();<br>
break;<br>
case 'whell':<br>
$instance=new busWhell();<br>
break;<br>
default:<br>
throw new Exception('巴士车工厂无法生产这种产品');<br>
}<br>
return $instance;<br>
}<br>
<br>}<br><br>
$car['engine']=carFactory::getInstance('engine')->engine();<br>
$car['body']=carFactory::getInstance('body')->body();<br>
$car['whell']=carFactory::getInstance('whell')->whell();<br>print_r($car);<br><br>
$bus['engine']=busFactory::getInstance('engine')->engine();<br>
$bus['body']=busFactory::getInstance('body')->body();<br>
$bus['whell']=busFactory::getInstance('whell')->whell();<br>print_r($bus);抽象工厂模式将工厂模式进行抽象,可以使得抽象出来的新结构更加的灵活。例如,若生产车身需要一个喷漆的动作,在工厂模式中,我们需要对整体结构进行更改,而抽象工厂中,只需要对生产车身进行更改就ok了。
抽象工厂模式同样具有工厂模式对结构要求高的缺点,整体结构的扩展或精简将变得更加的烦杂,所以使用抽象工厂模式时,对等级结构的划分是非常重要的。
以上就是php面向对象开发之——抽象工厂模式的内容,更多相关内容请关注PHP中文网(www.php.cn)!
相关文章:
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号