Abstract Factory Pattern is an abstraction of Factory Pattern. In layman’s terms, it separates the structure of the factory pattern into individuals that can operate independently.
Let’s take an example from the factory model to illustrate:
Now there is a car factory, which produces cars and buses. Cars and buses are composed of engines, bodies and wheels. of.
In the factory model, we regard cars and buses as two categories in the automobile family. The production of engines, bodies and wheels is the fixed structure of the production of cars, as shown in the following figure:
In the abstract factory pattern, the production engine, body and wheels are abstracted respectively, as shown in the following figure:
The actual deployment is:
//生产引擎的标准 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 '巴士车轮子'; } }
Continue to abstract the factory, abstract the car factory and the bus factory, and associate each factory with each component, as shown in the figure:
The actual deployment is:
The actual deployment is:
//生产引擎的标准 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 '巴士车轮子'; } }
//工厂标准
interface factory {
static public function getInstance($type);
}
//汽车工厂
class carFactory implements factory{
static public function getInstance($type){
$instance='';
switch($type){
case 'engine':
$instance=new carEngine();
break;
case 'body':
$instance=new carBody();
break;
case 'whell':
$instance=new carWhell();
break;
default:
throw new Exception('汽车工厂无法生产这种产品');
}
return $instance;
}
}
//巴士车工厂
class busFactory implements factory{
static public function getInstance($type){
$instance='';
switch($type){
case 'engine':
$instance=new busEngine();
break;
case 'body':
$instance=new busBody();
break;
case 'whell':
$instance=new busWhell();
break;
default:
throw new Exception('巴士车工厂无法生产这种产品');
}
return $instance;
}
}
$car['engine']=carFactory::getInstance('engine')->engine();
$car['body']=carFactory::getInstance('body')->body();
$car['whell']=carFactory::getInstance('whell')->whell();
print_r($car);
$bus['engine']=busFactory::getInstance('engine')->engine();
$bus['body']=busFactory::getInstance('body')->body();
$bus['whell']=busFactory::getInstance('whell')->whell();
print_r($bus);
Abstract factory pattern abstracts the factory pattern, which can make the abstracted new structure more flexible. For example, if the production of a car body requires a painting action, in the factory mode, we need to change the overall structure, but in the abstract factory, we only need to change the production body.
The abstract factory pattern also has the disadvantage that the factory pattern has high structural requirements. The expansion or simplification of the overall structure will become more complicated. Therefore, when using the abstract factory pattern, the division of the hierarchical structure is very important.
The above is the content of PHP object-oriented development - the abstract factory pattern. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Related articles:
Detailed explanation of the specific code to implement the abstract factory pattern in Java
Abstract Factory Pattern of JAVA Design Pattern
Comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern