建造者模式 - 设计模式 - PHP版
2021-05-18 13:27
标签:end net style 生成 foo this new param 生成器 建造者模式 - 设计模式 - PHP版 标签:end net style 生成 foo this new param 生成器 原文地址:http://www.cnblogs.com/benben7466/p/7736543.html 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}
";
17 echo "翅膀的颜色:{$this->_wing}
";
18 echo "脚的颜色:{$this->_foot}
";
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 "蓝鸟的组成:
";
93 $blue_bird = $director->Construct(new BlueBird());
94 $blue_bird->Show();
95 echo "
Rose鸟的组成:
";
96 $rose_bird = $director->Construct(new RoseBird());
97 $rose_bird->Show();
文章标题:建造者模式 - 设计模式 - PHP版
文章链接:http://soscw.com/index.php/essay/87199.html