建造者模式 - 設計模式 - PHP版
阿新 • • 發佈:2017-10-26
fun tail art lac 設計模式 http black color build
1 <?php 2 /* 3 * 建造者模式 4 * 5 * 參考:http://blog.csdn.net/jhq0113/article/details/45268743 6 * 7 */ 8 /* * 具體產品角色 鳥類 9 * Class Bird 10 */ 11 class Bird { 12 public $_head; 13 public $_wing; 14 public $_foot; 15 function show() { 16 echo "頭的顏色:{$this->_head}<br/>";17 echo "翅膀的顏色:{$this->_wing}<br/>"; 18 echo "腳的顏色:{$this->_foot}<br/>"; 19 } 20 } 21 /* * 抽象鳥的建造者(生成器) 22 * Class BirdBuilder 23 */ 24 abstract class BirdBuilder { 25 protected $_bird; 26 function __construct() { 27 $this->_bird = newBird(); 28 } 29 abstract function BuildHead(); 30 abstract function BuildWing(); 31 abstract function BuildFoot(); 32 abstract function GetBird(); 33 } 34 /* * 具體鳥的建造者(生成器) 藍鳥 35 * Class BlueBird 36 */ 37 class BlueBird extends BirdBuilder { 38 function BuildHead() {39 // TODO: Implement BuilderHead() method. 40 $this->_bird->_head = "Blue"; 41 } 42 function BuildWing() { 43 // TODO: Implement BuilderWing() method. 44 $this->_bird->_wing = "Blue"; 45 } 46 function BuildFoot() { 47 // TODO: Implement BuilderFoot() method. 48 $this->_bird->_foot = "Blue"; 49 } 50 function GetBird() { 51 // TODO: Implement GetBird() method. 52 return $this->_bird; 53 } 54 } 55 /* * 玫瑰鳥 56 * Class RoseBird 57 */ 58 class RoseBird extends BirdBuilder { 59 function BuildHead() { 60 // TODO: Implement BuildHead() method. 61 $this->_bird->_head = "Red"; 62 } 63 function BuildWing() { 64 // TODO: Implement BuildWing() method. 65 $this->_bird->_wing = "Black"; 66 } 67 function BuildFoot() { 68 // TODO: Implement BuildFoot() method. 69 $this->_bird->_foot = "Green"; 70 } 71 function GetBird() { 72 // TODO: Implement GetBird() method. 73 return $this->_bird; 74 } 75 } 76 /* * 指揮者 77 * Class Director 78 */ 79 class Director { 80 /** 81 * @param $_builder 建造者 82 * @return mixed 產品類:鳥 83 */ 84 function Construct($_builder) { 85 $_builder->BuildHead(); 86 $_builder->BuildWing(); 87 $_builder->BuildFoot(); 88 return $_builder->GetBird(); 89 } 90 } 91 $director = new Director(); 92 echo "藍鳥的組成:<hr/>"; 93 $blue_bird = $director->Construct(new BlueBird()); 94 $blue_bird->Show(); 95 echo "<br/>Rose鳥的組成:<hr/>"; 96 $rose_bird = $director->Construct(new RoseBird()); 97 $rose_bird->Show();
建造者模式 - 設計模式 - PHP版