ホームページ  >  記事  >  バックエンド開発  >  PHP オブジェクト指向開発 - 抽象ファクトリー パターン

PHP オブジェクト指向開発 - 抽象ファクトリー パターン

黄舟
黄舟オリジナル
2017-03-18 10:49:292077ブラウズ

Abstract Factory Pattern は、Factory Pattern を抽象化したもので、平たく言えば、Factory Pattern の構造を独立して実行できる個別のものに分離することを意味します。

説明するために工場モデルの例を見てみましょう:

今、自動車とバスを生産する自動車工場があります。自動車とバスはエンジン、車体、車輪で構成されています。

以下の図に示すように、工場パターンでは、自動車とバスを自動車ファミリーの 2 つのカテゴリとして捉え、エンジン、ボディ、ホイールの生産は自動車を生産するための固定構造です。パターンでは、以下の図に示すように、生産エンジン、車体、車輪をそれぞれ抽象化します: 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 '巴士车轮子';
	}
	
}

//工厂标准
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);

抽象的なファクトリ パターン ファクトリ パターンを抽象化すると、抽象化された新しい構造をより柔軟にすることができます。たとえば、車体の生産に塗装アクションが必要な場合、ファクトリーモードでは全体の構造を変更する必要がありますが、抽象ファクトリーでは生産車体を変更するだけで済みます。

ファクトリ パターンには構造要件が高いという欠点もあります。そのため、全体の構造の拡張または合理化がより複雑になるため、抽象ファクトリ パターンを使用する場合は、階層構造の分割が非常に重要です。


上記は、PHP オブジェクト指向開発 - 抽象ファクトリー パターンの内容です。さらに関連した内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください。

関連記事:

Javaで抽象ファクトリパターンを実装するための具体的なコードを詳しく解説

JAVAデザインパターンの抽象ファクトリパターン

PHPのシンプルファクトリパターン、ファクトリメソッドパターン、抽象の比較ファクトリーパターン

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。