在学习本章之前,请先阅读简单工厂模式以及工厂方法模式
在什么情况下应当使用抽象工厂模式:
1、一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有的形态的工厂模式都是重要的。
2、这个系统的产品有多余一个的产品族,而系统只消费其中某一个族的产品。
3、同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。
4、系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。
案例1:
还是以农场为例。
我们的农场分了多个产品线,一个是专门卖北方的蔬菜水果。一个专门卖南方的蔬菜水果。大家可以试着不写一下,这里就不给出示例了,uml类图大家可以试着画一下。
案例2:
以电脑为例
电脑分为PC电脑,mac电脑。pc电脑有他组装需要的cpu、主板、键盘 | mac电脑同样需要这些组件,但是型号不一样,下面是此示例的uml类图。
代码示例:
此示例是依据女娲造人而写的。
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>//</span><span>此实例是根据女娲造人写的 </span><span> 4</span> <span> 5</span> <span>//首先定义一个全局的工厂接口 </span><span> 6</span> <span>//由阴绳 阳绳来继承 都要实现举绳子这个方法</span> <span> 7</span> <span>interface</span><span> nvwaRope{ </span><span> 8</span> <span>public</span> <span>function</span> liftPeopleRope(<span>$name</span><span>); </span><span> 9</span> <span>public</span> <span>function</span> liftAnimalRope(<span>$name</span><span>); </span><span> 10</span> <span>} </span><span> 11</span> <span> 12</span> <span>//</span><span>定义阴绳 来继承 nvwaRope</span> <span> 13</span> <span>class</span> yinRope <span>implements</span><span> nvwaRope{ </span><span> 14</span> <span>public</span> <span>function</span> liftPeopleRope(<span>$name</span><span>){ </span><span> 15</span> <span>return</span> <span>new</span> girl(<span>$name</span><span>); </span><span> 16</span> <span> } </span><span> 17</span> <span> 18</span> <span>public</span> <span>function</span> liftAnimalRope(<span>$name</span><span>){ </span><span> 19</span> <span>return</span> <span>new</span> muAnimal(<span>$name</span><span>); </span><span> 20</span> <span> } </span><span> 21</span> <span>} </span><span> 22</span> <span> 23</span> <span>//</span><span>定义阳绳 </span> <span> 24</span> <span>class</span> yangRope <span>implements</span><span> nvwaRope{ </span><span> 25</span> <span>public</span> <span>function</span> liftPeopleRope(<span>$name</span><span>){ </span><span> 26</span> <span>return</span> <span>new</span> boy(<span>$name</span><span>); </span><span> 27</span> <span> } </span><span> 28</span> <span> 29</span> <span>public</span> <span>function</span> liftAnimalRope(<span>$name</span><span>){ </span><span> 30</span> <span>return</span> <span>new</span> gongAnimal(<span>$name</span><span>); </span><span> 31</span> <span> } </span><span> 32</span> <span>} </span><span> 33</span> <span> 34</span> <span> 35</span> <span>//</span><span>定义一个人的抽象接口</span> <span> 36</span> <span>interface</span><span> people{ </span><span> 37</span> <span>public</span> <span>function</span><span> eat(); </span><span> 38</span> <span>public</span> <span>function</span><span> getName(); </span><span> 39</span> <span>} </span><span> 40</span> <span> 41</span> <span> 42</span> <span>abstract</span> <span>class</span> abstractPeople <span>implements</span><span> people{ </span><span> 43</span> <span>private</span> <span>$name</span><span>; </span><span> 44</span> <span> 45</span> <span>public</span> <span>function</span> __construct(<span>$name</span><span>){ </span><span> 46</span> <span>$this</span>->name = <span>$name</span><span>; </span><span> 47</span> <span> } </span><span> 48</span> <span> 49</span> <span>public</span> <span>function</span><span> eat(){ </span><span> 50</span> <span>echo</span> <span>$this</span>->name . " eat"<span>; </span><span> 51</span> <span> } </span><span> 52</span> <span> 53</span> <span>public</span> <span>function</span><span> getName(){ </span><span> 54</span> <span>echo</span> <span>$this</span>-><span>name; </span><span> 55</span> <span>return</span> <span>$this</span>-><span>name; </span><span> 56</span> <span> } </span><span> 57</span> <span> 58</span> <span>//</span><span>检查是否有胸</span> <span> 59</span> <span>public</span> <span>function</span><span> checkChest(){ </span><span> 60</span> <span>return</span> <span>$this</span>-><span>haveChest; </span><span> 61</span> <span> } </span><span> 62</span> <span>} </span><span> 63</span> <span> 64</span> <span>class</span> boy <span>extends</span><span> abstractPeople{ </span><span> 65</span> <span>public</span> <span>$haveChest</span> = <span>false</span>; <span>//</span><span>男孩没有胸</span> <span> 66</span> <span>} </span><span> 67</span> <span> 68</span> <span>class</span> girl <span>extends</span><span> abstractPeople{ </span><span> 69</span> <span>public</span> <span>$haveChest</span> = <span>true</span>; <span>//</span><span>女孩有胸</span> <span> 70</span> <span>} </span><span> 71</span> <span> 72</span> <span> 73</span> <span>//</span><span>定义一个动物的抽象接口</span> <span> 74</span> <span>interface</span><span> animal{ </span><span> 75</span> <span>public</span> <span>function</span><span> eat(); </span><span> 76</span> <span>public</span> <span>function</span> <span>sleep</span><span>(); </span><span> 77</span> <span>} </span><span> 78</span> <span> 79</span> <span>abstract</span> <span>class</span> abstractAnimal <span>implements</span><span> animal{ </span><span> 80</span> <span>private</span> <span>$name</span><span>; </span><span> 81</span> <span> 82</span> <span>public</span> <span>function</span> __construct(<span>$name</span><span>){ </span><span> 83</span> <span>$this</span>->name = <span>$name</span><span>; </span><span> 84</span> <span> } </span><span> 85</span> <span> 86</span> <span>public</span> <span>function</span><span> eat(){ </span><span> 87</span> <span>echo</span> <span>$this</span>->name . " eating"<span>; </span><span> 88</span> <span> } </span><span> 89</span> <span> 90</span> <span>public</span> <span>function</span> <span>sleep</span><span>(){ </span><span> 91</span> <span>echo</span> <span>$this</span>->name . " sleeping"<span>; </span><span> 92</span> <span> } </span><span> 93</span> <span> 94</span> <span>public</span> <span>function</span><span> getName(){ </span><span> 95</span> <span>echo</span> <span>$this</span>-><span>name; </span><span> 96</span> <span>return</span> <span>$this</span>-><span>name; </span><span> 97</span> <span> } </span><span> 98</span> <span>} </span><span> 99</span> <span>100</span> <span>//</span><span>定义一个母动物的类</span> <span>101</span> <span>class</span> muAnimal <span>extends</span><span> abstractAnimal{ </span><span>102</span> <span>public</span> <span>$xingbie</span> = "mu"<span>; </span><span>103</span> <span>} </span><span>104</span> <span>105</span> <span>106</span> <span>//</span><span>定义一个公动物的类</span> <span>107</span> <span>class</span> gongAnimal <span>extends</span><span> abstractAnimal{ </span><span>108</span> <span>public</span> <span>$xingbie</span> = "gong"<span>; </span><span>109</span> <span>} </span><span>110</span> <span>111</span> <span>112</span> <span>113</span> <span>//</span><span>使用方式 </span><span>114</span> <span>115</span> <span>//首先获取工厂的实例 也就是阴绳或者阳绳的实例</span> <span>116</span> <span>$yinRope</span> = <span>new</span><span> yinRope(); </span><span>117</span> <span>118</span> <span>$people</span> = <span>$yinRope</span>->liftPeopleRope("zhangsan"<span>); </span><span>119</span> <span>echo</span> <span>$people</span>-><span>eat(); </span><span>120</span> <span>echo</span> <span>$people</span>-><span>getName(); </span><span>121</span> <span>var_dump</span>(<span>$people</span>-><span>checkChest()); </span><span>122</span> <span>123</span> <span>$animel</span> = <span>$yinRope</span>->liftAnimalRope("马"<span>); </span><span>124</span> <span>echo</span> <span>$animel</span>->eat();
如果使用UserFactory,你就不用知道有User类的存在,抽象类帮你隐藏它,以后要是类多了,该模式维护起来就比较方便,建议你去了解抽象工厂模式,工厂模式,工厂方法模式,这些都是为了可复用编程的手段。
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。
区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。