Heim > Artikel > Backend-Entwicklung > php设计模式 — 建造者模式,php设计模式_PHP教程
需求分析:
我们接到了一个订单,是宝马公司和奔驰公司的,他们负责定义产品的零部件以及型号,我们负责生产,需求简单的描述就是这样。 我们需要为这个需求设计一个设计模式去更好的适应他们的需求。
首先我们需要一个车模型类,来定义好需要的所有零部件,这就叫做抽象类,之所以这样是因为我们还有可能接到更多公司的订单,比如劳斯莱斯,宾利。
然后由各自的车来继承这个抽象类,实现里面的方法。
接下来就需要一个建造者抽象类,来定义建造各自的车需要的方法
然后由各自车建造者来继承这个抽象类。
我们会想到一个建造模式了,不错,就是建造者模式。用它简直太合适了。看一下建造者的用例图
请注意:此图示例方法部分和我的示例对不上哦。
直接上代码:
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>abstract</span> <span>class</span><span> carModel{ </span><span> 4</span> <span> 5</span> <span>//</span><span>这里存储所有组装车需要的零件</span> <span> 6</span> <span>public</span> <span>$spareParts</span> = <span>array</span><span>(); </span><span> 7</span> <span> 8</span> <span>//</span><span>车的名字</span> <span> 9</span> <span>public</span> <span>$carName</span> = ""<span>; </span><span> 10</span> <span> 11</span> <span>//</span><span>增加轮子部件</span> <span> 12</span> <span>public</span> <span>abstract</span> <span>function</span> addLunzi(<span>$xinghao</span><span>); </span><span> 13</span> <span> 14</span> <span>//</span><span>增加外壳部件</span> <span> 15</span> <span>public</span> <span>abstract</span> <span>function</span> addWaike(<span>$xinghao</span><span>); </span><span> 16</span> <span> 17</span> <span>//</span><span>增加发动机部件</span> <span> 18</span> <span>public</span> <span>abstract</span> <span>function</span> addFadongji(<span>$xinghao</span><span>); </span><span> 19</span> <span> 20</span> <span>//</span><span>获取车,并给车取名字</span> <span> 21</span> <span>final</span> <span>public</span> <span>function</span> getCar(<span>$carName</span><span>){ </span><span> 22</span> <span>if</span>(<span>$this</span>-><span>spareParts){ </span><span> 23</span> <span>$this</span>->carName = <span>$carName</span><span>; </span><span> 24</span> <span>//</span><span>$k 代表部件名字 </span><span> 25</span> <span> //$v 代表型号</span> <span> 26</span> <span>foreach</span>(<span>$this</span>->spareParts <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span> 27</span> <span>$actionName</span> = "add" . <span>$k</span><span>; </span><span> 28</span> <span>$this</span>-><span>$actionName</span>(<span>$v</span><span>); </span><span> 29</span> <span> } </span><span> 30</span> }<span>else</span><span>{ </span><span> 31</span> <span>throw</span> <span>new</span> <span>Exception</span>("没有汽车部件"<span>); </span><span> 32</span> <span> 33</span> <span> } </span><span> 34</span> <span> } </span><span> 35</span> <span>} </span><span> 36</span> <span> 37</span> <span> 38</span> <span>//</span><span>定义具体的产品</span> <span> 39</span> <span>class</span> bmwCarModel <span>extends</span><span> carModel{ </span><span> 40</span> <span> 41</span> <span>public</span> <span>$spareParts</span> = <span>array</span><span>(); </span><span> 42</span> <span>public</span> <span>$carName</span> = ""<span>; </span><span> 43</span> <span> 44</span> <span>public</span> <span>function</span> addLunzi(<span>$xinghao</span><span>){ </span><span> 45</span> <span>echo</span> "宝马".<span>$this</span>->carName."的轮子,型号是" . <span>$xinghao</span> . "\n"<span>; </span><span> 46</span> <span> } </span><span> 47</span> <span> 48</span> <span>public</span> <span>function</span> addWaike(<span>$xinghao</span><span>){ </span><span> 49</span> <span>echo</span> "宝马".<span>$this</span>->carName."的外壳,型号是" . <span>$xinghao</span> . "\n"<span>; </span><span> 50</span> <span> } </span><span> 51</span> <span> 52</span> <span>public</span> <span>function</span> addFadongji(<span>$xinghao</span><span>){ </span><span> 53</span> <span>echo</span> "宝马".<span>$this</span>->carName."的发动机,型号是 " . <span>$xinghao</span> . "\n"<span>; </span><span> 54</span> <span> } </span><span> 55</span> <span>} </span><span> 56</span> <span> 57</span> <span> 58</span> <span>//</span><span>定义具体的产品</span> <span> 59</span> <span>class</span> benziCarModel <span>extends</span><span> carModel{ </span><span> 60</span> <span> 61</span> <span>public</span> <span>$spareParts</span> = <span>array</span><span>(); </span><span> 62</span> <span>public</span> <span>$carName</span> = ""<span>; </span><span> 63</span> <span> 64</span> <span>public</span> <span>function</span> addLunzi(<span>$xinghao</span><span>){ </span><span> 65</span> <span>echo</span> "奔驰".<span>$this</span>->carName."的轮子,型号是" . <span>$xinghao</span> . "\n"<span>; </span><span> 66</span> <span> } </span><span> 67</span> <span> 68</span> <span>public</span> <span>function</span> addWaike(<span>$xinghao</span><span>){ </span><span> 69</span> <span>echo</span> "奔驰".<span>$this</span>->carName."的外壳,型号是" . <span>$xinghao</span> . "\n"<span>; </span><span> 70</span> <span> } </span><span> 71</span> <span> 72</span> <span>public</span> <span>function</span> addFadongji(<span>$xinghao</span><span>){ </span><span> 73</span> <span>echo</span> "奔驰".<span>$this</span>->carName."的发动机,型号是 " . <span>$xinghao</span> . "\n"<span>; </span><span> 74</span> <span> } </span><span> 75</span> <span>} </span><span> 76</span> <span> 77</span> <span> 78</span> <span> 79</span> <span>//</span><span>定义建造者</span> <span> 80</span> <span>abstract</span> <span>class</span><span> carBuilder{ </span><span> 81</span> <span>public</span> <span>abstract</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>); </span><span> 82</span> <span> 83</span> <span>public</span> <span>abstract</span> <span>function</span> getCarModel(<span>$name</span><span>); </span><span> 84</span> <span>} </span><span> 85</span> <span> 86</span> <span> 87</span> <span>class</span> bmwBuilder <span>extends</span><span> carBuilder{ </span><span> 88</span> <span>private</span> <span>$bmwModel</span><span>; </span><span> 89</span> <span> 90</span> <span>public</span> <span>function</span><span> __construct(){ </span><span> 91</span> <span>$this</span>->bmwModel = <span>new</span><span> bmwCarModel(); </span><span> 92</span> <span> } </span><span> 93</span> <span> 94</span> <span>public</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>){ </span><span> 95</span> <span>$this</span>->bmwModel->spareParts[<span>$partsName</span>] = <span>$xinghao</span><span>; </span><span> 96</span> <span> } </span><span> 97</span> <span> 98</span> <span>public</span> <span>function</span> getCarModel(<span>$name</span><span>){ </span><span> 99</span> <span>$this</span>->bmwModel->getCar(<span>$name</span><span>); </span><span>100</span> <span> } </span><span>101</span> <span>} </span><span>102</span> <span>103</span> <span>104</span> <span>class</span> benziBuilder <span>extends</span><span> carBuilder{ </span><span>105</span> <span>private</span> <span>$benziModel</span><span>; </span><span>106</span> <span>107</span> <span>public</span> <span>function</span><span> __construct(){ </span><span>108</span> <span>$this</span>->benziModel = <span>new</span><span> benziCarModel(); </span><span>109</span> <span> } </span><span>110</span> <span>111</span> <span>public</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>){ </span><span>112</span> <span>$this</span>->bmwModel->spareParts[<span>$partsName</span>] = <span>$xinghao</span><span>; </span><span>113</span> <span> } </span><span>114</span> <span>115</span> <span>public</span> <span>function</span> getCarModel(<span>$name</span><span>){ </span><span>116</span> <span>$this</span>->bmwModel->getCar(<span>$name</span><span>); </span><span>117</span> <span> } </span><span>118</span> <span>} </span><span>119</span> <span>120</span> <span>121</span> <span>122</span> <span>//</span><span>模拟客户端调用 </span><span>123</span> <span>124</span> <span>//创建一辆宝马车,取名字为宝马x1</span> <span>125</span> <span>126</span> <span>$bmwBuilder</span> = <span>new</span><span> bmwBuilder(); </span><span>127</span> <span>$bmwBuilder</span>->setSpareParts('Lunzi' , '牛逼轮子1号'<span>); </span><span>128</span> <span>$bmwBuilder</span>->setSpareParts('Waike' , '牛逼外壳1号'<span>); </span><span>129</span> <span>$bmwBuilder</span>->setSpareParts('Fadongji' , '牛逼发动机1号'<span>); </span><span>130</span> <span>$bmwBuilder</span>->getCarModel("宝马x1"<span>); </span><span>131</span> <span>$bmwBuilder</span>->getCarModel("宝马x1"); <span>//</span><span>连续创建两个宝马x1 </span><span>132</span> <span>133</span> <span>//再创建一个宝马 没有外壳 取名为 宝马s5</span> <span>134</span> <span>$bmwBuilder</span> = <span>new</span><span> bmwBuilder(); </span><span>135</span> <span>$bmwBuilder</span>->setSpareParts('Lunzi' , '牛逼轮子2号'<span>); </span><span>136</span> <span>$bmwBuilder</span>->setSpareParts('Fadongji' , '牛逼发动机2号'<span>); </span><span>137</span> <span>$bmwBuilder</span>->getCarModel("宝马s5"<span>); </span><span>138</span> <span>$bmwBuilder</span>->getCarModel("宝马s5"); <span>//</span><span>连续创建两个宝马x1</span>
代码可以直接运行,你可以试着去生产一辆牛逼的奔驰车哦。
建造者模式(Builder Pattern)也叫做生成器模式,其定义如下:
Separate the construction of a complex object from its representation so that the same construction process can create different representations. 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式的通用类图如图所示。
在建造者模式中,有如下四个角色:
通常是实现了模板方法模式,也就是有模板方法和基本方法,这个参考上一章节的模板方法模式。在例子中,BenzModel和BMWModel就属于产品类。
规范产品的组建,一般是由子类实现。在例子中,CarBuilder属于抽象建造者。
实现抽象类定义的所有方法,并且返回一个组件好的对象。在例子中,BenzBuilder和BMWBuilder就属于具体建造者。
负责安排已有模块的顺序,然后告诉Builder开始建造,在上面的例子中就是我们的老大,牛叉公司找到老大,说我要这个,这个,那个类型的车辆模型,然后老大就把命令传递给我,我和我的团队就开始拼命的建造,于是一个项目建设完毕了。
你不必特别去找'php设计模式',你可以找找‘设计模式’or‘java设计模式’之类的比较好找。因为设计模式并不是针对某个语言而是一种思想,所以你无论是看'php设计模式'or‘java设计模式’or‘设计模式’得到的设计思想都是一样的
如果使用UserFactory,你就不用知道有User类的存在,抽象类帮你隐藏它,以后要是类多了,该模式维护起来就比较方便,建议你去了解抽象工厂模式,工厂模式,工厂方法模式,这些都是为了可复用编程的手段。