欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php面向对象开发之——抽象工厂模式

程序员文章站 2022-05-01 09:11:42
...
抽象工厂模式是对工厂模式的抽象,通俗来说,就是把工厂模式的结构分离出来成为能独立运行的个体。

还是拿工厂模式中的例子来说明:

现在有一个汽车工厂,它生产小汽车和巴士车,小汽车和巴士车都是由引擎、车身和*组成的。

在工厂模式中,我们把小汽车和巴士车作为汽车族群中的两个类别,生产引擎、车身和*为生产汽车的固定结构,如下图所示:php面向对象开发之——抽象工厂模式

在抽象工厂模式中,把生产引擎、车身和*分别抽象出来,如下图所示:

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 '巴士车*';
	}
	
}

再继续对工厂进行抽象,抽象出汽车工厂和巴士车工厂,并且让各工厂与各组件相关联,如图:

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)!

相关文章:

Java 实现抽象工厂模式的具体代码详解

JAVA设计模式之抽象工厂模式

PHP简单工厂模式、工厂方法模式和抽象工厂模式比较