码迷,mamicode.com
首页 > Web开发 > 详细

建造者模式 - 设计模式 - PHP版

时间:2017-10-26 15:37:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:end   net   style   生成   foo   this   new   param   生成器   

 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 = new Bird();
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版

标签:end   net   style   生成   foo   this   new   param   生成器   

原文地址:http://www.cnblogs.com/benben7466/p/7736543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!