php设计模式--生成器模式
生成器模式
require "D:\\xxx\bild.php"; require "D:\\xxx\cx_bild.php"; require "D:\\xxx\jt_build.php"; class Director { public function build($build){ $build->buildHeader(); $build->buildChiBang(); $build->buildJiao(); return $build->GetBird(); } } $dir = new Director(); $duixiang = $dir->build(new BuleBild()); $Zabild = $dir->build(new Zabild()); $BuleBild = $duixiang->show(); $Zabild = $Zabild->show();
bild.php
<?php class bild { public $header; public $chibang; public $jiao; function show(){ echo '头的颜色'.$this->header."<br>"; echo '翅膀的颜色'.$this->chibang."<br>"; echo '叫的颜色'.$this->jiao."<br>"; } }
cx_build.php
<?php abstract class BildBuilder{ public $bild; public function __construct() { $this->bild = new bild(); } abstract function buildHeader(); abstract function buildChiBang(); abstract function buildJiao(); }
jt_build.php
<?php class BuleBild extends BildBuilder{ public function buildHeader(){ $this->bild->header = 'bule'; } public function buildChiBang(){ $this->bild->chibang = 'bule'; } public function buildJiao(){ $this->bild->jiao = 'bule'; } public function GetBird(){ return $this->bild; } } class Zabild extends BildBuilder{ public function buildHeader(){ $this->bild->header = 'Red'; } public function buildChiBang(){ $this->bild->chibang = 'huang'; } public function buildJiao(){ $this->bild->jiao = 'qings'; } public function GetBird(){ return $this->bild; } }