PHP 抽象ファクトリー パターン開発のアイデア

WBOY
リリース: 2016-06-20 13:00:54
オリジナル
776 人が閲覧しました

PHP 抽象ファクトリー パターンの開発アイデア

抽象ファクトリ パターンはファクトリ パターンに相対的です

抽象ファクトリー パターンは、ファクトリー パターンを抽象化したもので、平たく言えば、ファクトリー パターンの構造を独立して動作できる個々の要素に分割します。

工場出荷時のモデルから例を挙げて説明しましょう:

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

ファクトリーモデルでは、以下の図に示すように、自動車とバスを自動車グループの 2 つのカテゴリとして扱います。エンジン、ボディ、ホイールの製造は、自動車を生産するための固定構造です。

抽象ファクトリー パターンでは、次の図に示すように、生産エンジン、ボディ、ホイールが個別に抽象化されます。

実際のデプロイメントは次のとおりです:

//生产引擎的标准
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 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.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!