php设计模式 — 抽象工厂模式,设计模式工厂
php设计模式 — 抽象工厂模式,设计模式工厂
在学习本章之前,请先阅读简单工厂模式以及工厂方法模式
在什么情况下应当使用抽象工厂模式:
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类的存在,抽象类帮你隐藏它,以后要是类多了,该模式维护起来就比较方便,建议你去了解抽象工厂模式,工厂模式,工厂方法模式,这些都是为了可复用编程的手段。
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。
区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.
