Home  >  Article  >  Backend Development  >  PHP design pattern - simple factory pattern (static factory method pattern), PHP design pattern_PHP tutorial

PHP design pattern - simple factory pattern (static factory method pattern), PHP design pattern_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:34796browse

php design pattern - simple factory pattern (static factory method pattern), php design pattern

Concept

Simple Factory Pattern [Static Factory Method Pattern]
is a class creation pattern

Several forms of factory pattern:
1. Simple Factory Pattern (Simple Factory) | Also called Static Factory Method Pattern (Static Factory Method)
2. Factory Method | Also called Polymorphic Factory
3. Abstract Factory Pattern | Also called ToolKit

With pictures

Code Example

Just run the code directly, it has been tested

<span>  1</span> <?<span>php
</span><span>  2</span> 
<span>  3</span> <span>/*</span><span>*
</span><span>  4</span> <span> * 一个事例
</span><span>  5</span> <span> *
</span><span>  6</span> <span> * 一个农场,要向市场销售水果
</span><span>  7</span> <span> * 农场里有三种水果 苹果、葡萄
</span><span>  8</span> <span> * 我们设想:1、水果有多种属性,每个属性都有不同,但是,他们有共同的地方 |  生长、种植、收货、吃
</span><span>  9</span> <span> *              2、将来有可能会增加新的水果、我们需要定义一个接口来规范他们必须实现的方法
</span><span> 10</span> <span> *              3、我们需要获取某个水果的类,要从农场主那里去获取某个水果的实例,来知道如何生长、种植、收货、吃
</span><span> 11</span>  <span>*/</span>
<span> 12</span> 
<span> 13</span> 
<span> 14</span> <span>/*</span><span>*
</span><span> 15</span> <span> * 虚拟产品接口类
</span><span> 16</span> <span> * 定义好需要实现的方法
</span><span> 17</span>  <span>*/</span>
<span> 18</span> 
<span> 19</span> <span>interface</span><span> fruit{
</span><span> 20</span> 
<span> 21</span>     <span>/*</span><span>*
</span><span> 22</span> <span>     * 生长
</span><span> 23</span>      <span>*/</span>
<span> 24</span>     <span>public</span> <span>function</span><span> grow();
</span><span> 25</span> 
<span> 26</span>     <span>/*</span><span>*
</span><span> 27</span> <span>     * 种植
</span><span> 28</span>      <span>*/</span>
<span> 29</span>     <span>public</span> <span>function</span><span> plant();
</span><span> 30</span> 
<span> 31</span>     <span>/*</span><span>*
</span><span> 32</span> <span>     * 收获
</span><span> 33</span>      <span>*/</span>
<span> 34</span>     <span>public</span> <span>function</span><span> harvest();
</span><span> 35</span> 
<span> 36</span>     <span>/*</span><span>*
</span><span> 37</span> <span>     * 吃
</span><span> 38</span>      <span>*/</span>
<span> 39</span>     <span>public</span> <span>function</span><span> eat();
</span><span> 40</span>     
<span> 41</span> <span>}
</span><span> 42</span> 
<span> 43</span> 
<span> 44</span> <span>/*</span><span>*
</span><span> 45</span> <span> * 定义具体产品类 苹果
</span><span> 46</span> <span> * 首先,我们要实现所继承的接口所定义的方法
</span><span> 47</span> <span> * 然后定义苹果所特有的属性,以及方法
</span><span> 48</span>  <span>*/</span>
<span> 49</span> <span>class</span> apple <span>implements</span><span> fruit{
</span><span> 50</span> 
<span> 51</span>     <span>//</span><span>苹果树有年龄</span>
<span> 52</span>     <span>private</span> <span>$treeAge</span><span>;
</span><span> 53</span> 
<span> 54</span>     <span>//</span><span>苹果有颜色</span>
<span> 55</span>     <span>private</span> <span>$color</span><span>;
</span><span> 56</span> 
<span> 57</span>     <span>public</span> <span>function</span><span> grow(){
</span><span> 58</span>         <span>echo</span> "grape grow"<span>;
</span><span> 59</span> <span>    }
</span><span> 60</span> 
<span> 61</span>     <span>public</span> <span>function</span><span> plant(){
</span><span> 62</span>         <span>echo</span> "grape plant"<span>;
</span><span> 63</span> <span>    }
</span><span> 64</span> 
<span> 65</span>     <span>public</span> <span>function</span><span> harvest(){
</span><span> 66</span>         <span>echo</span> "grape harvest"<span>;
</span><span> 67</span> <span>    }
</span><span> 68</span> 
<span> 69</span>     <span>public</span> <span>function</span><span> eat(){
</span><span> 70</span>         <span>echo</span> "grape eat"<span>;
</span><span> 71</span> <span>    }
</span><span> 72</span> 
<span> 73</span>     <span>//</span><span>取苹果树的年龄</span>
<span> 74</span>     <span>public</span> <span>function</span><span> getTreeAge(){
</span><span> 75</span>         <span>return</span> <span>$this</span>-><span>treeAge;
</span><span> 76</span> <span>    }
</span><span> 77</span> 
<span> 78</span>     <span>//</span><span>设置苹果树的年龄</span>
<span> 79</span>     <span>public</span> <span>function</span> setTreeAge(<span>$age</span><span>){
</span><span> 80</span>         <span>$this</span>->treeAge = <span>$age</span><span>;
</span><span> 81</span>         <span>return</span><span> trie;
</span><span> 82</span> <span>    }
</span><span> 83</span> 
<span> 84</span> <span>}
</span><span> 85</span> 
<span> 86</span> <span>/*</span><span>*
</span><span> 87</span> <span> * 定义具体产品类 葡萄
</span><span> 88</span> <span> * 首先,我们要实现所继承的接口所定义的方法
</span><span> 89</span> <span> * 然后定义葡萄所特有的属性,以及方法
</span><span> 90</span>  <span>*/</span>
<span> 91</span> <span>class</span> grape <span>implements</span><span> fruit{
</span><span> 92</span> 
<span> 93</span> 
<span> 94</span>     <span>//</span><span>葡萄是否有籽</span>
<span> 95</span>     <span>private</span> <span>$seedLess</span><span>;
</span><span> 96</span> 
<span> 97</span>     <span>public</span> <span>function</span><span> grow(){
</span><span> 98</span>         <span>echo</span> "apple grow"<span>;
</span><span> 99</span> <span>    }
</span><span>100</span> 
<span>101</span>     <span>public</span> <span>function</span><span> plant(){
</span><span>102</span>         <span>echo</span> "apple plant"<span>;
</span><span>103</span> <span>    }
</span><span>104</span> 
<span>105</span>     <span>public</span> <span>function</span><span> harvest(){
</span><span>106</span>         <span>echo</span> "apple harvest"<span>;
</span><span>107</span> <span>    }
</span><span>108</span> 
<span>109</span>     <span>public</span> <span>function</span><span> eat(){
</span><span>110</span>         <span>echo</span> "apple eat"<span>;
</span><span>111</span> <span>    }
</span><span>112</span> 
<span>113</span>     <span>//</span><span>有无籽取值</span>
<span>114</span>     <span>public</span> <span>function</span><span> getSeedLess(){
</span><span>115</span>         <span>return</span> <span>$this</span>-><span>seedLess;
</span><span>116</span> <span>    }
</span><span>117</span> 
<span>118</span>     <span>//</span><span>设置有籽无籽</span>
<span>119</span>     <span>public</span> <span>function</span> setSeedLess(<span>$seed</span><span>){
</span><span>120</span>         <span>$this</span>->seedLess = <span>$seed</span><span>;
</span><span>121</span>         <span>return</span> <span>true</span><span>;
</span><span>122</span> <span>    }
</span><span>123</span> 
<span>124</span> <span>}
</span><span>125</span> 
<span>126</span> 
<span>127</span> <span>/*</span><span>*
</span><span>128</span> <span> *农场主类 用来获取实例化的水果
</span><span>129</span> <span> *
</span><span>130</span>  <span>*/</span>
<span>131</span> <span>class</span><span> farmer{
</span><span>132</span> 
<span>133</span>     <span>//</span><span>定义个静态工厂方法</span>
<span>134</span>     <span>public</span> <span>static</span> <span>function</span> factory(<span>$fruitName</span><span>){
</span><span>135</span>         <span>switch</span> (<span>$fruitName</span><span>) {
</span><span>136</span>             <span>case</span> 'apple':
<span>137</span>                 <span>return</span> <span>new</span><span> apple();
</span><span>138</span>                 <span>break</span><span>;
</span><span>139</span>             <span>case</span> 'grape':
<span>140</span>                 <span>return</span> <span>new</span><span> grape();
</span><span>141</span>                 <span>break</span><span>;
</span><span>142</span>             <span>default</span>:
<span>143</span>                 <span>throw</span> <span>new</span> badFruitException("Error no the fruit", 1<span>);
</span><span>144</span>                 <span>break</span><span>;
</span><span>145</span> <span>        }
</span><span>146</span> <span>    }
</span><span>147</span> <span>}
</span><span>148</span> 
<span>149</span> <span>class</span> badFruitException <span>extends</span> <span>Exception</span><span>{
</span><span>150</span>     <span>public</span> <span>$msg</span><span>;
</span><span>151</span>     <span>public</span> <span>$errType</span><span>;
</span><span>152</span>     <span>public</span> <span>function</span> __construct(<span>$msg</span> = '' , <span>$errType</span> = 1<span>){
</span><span>153</span>         <span>$this</span>->msg = <span>$msg</span><span>;
</span><span>154</span>         <span>$this</span>->errType = <span>$errType</span><span>;
</span><span>155</span> <span>    }    
</span><span>156</span> <span>}
</span><span>157</span> 
<span>158</span> 
<span>159</span> <span>/*</span><span>*
</span><span>160</span> <span> * 获取水果实例化的方法
</span><span>161</span>  <span>*/</span>
<span>162</span> <span>try</span><span>{
</span><span>163</span>     <span>$appleInstance</span> = farmer::factory('apple'<span>);
</span><span>164</span>     <span>var_dump</span>(<span>$appleInstance</span><span>);
</span><span>165</span> }<span>catch</span>(badFruitException <span>$err</span><span>){
</span><span>166</span>     <span>echo</span> <span>$err</span>->msg . "_______" . <span>$err</span>-><span>errType;
</span><span>167</span> }

php design pattern: Write PHP5 sample code for factory mode and monomorphic mode

Example #1 Call factory method (with parameters)

3920f92cfe6f31a5110b89f0567aa131
------------ --------------------------
Example #2 Singleton mode

6de74f351f5f111896a4fd7ff68948fc

What are the similarities and differences between the simple factory pattern and the factory method pattern?

It is recommended that LZ study simple factories, factory methods and abstract factories together. Haha, my teacher asked me to study the differences between these three modes some time ago. It may be clearer if we study together.
Simple factory pattern is also called static factory method pattern. It can be seen from the renaming that this mode must be very simple. Its purpose is simple: to define an interface for creating objects.
Let’s take a look at its composition first:
1) Factory role: This is the core of this model and contains certain business logic and judgment logic. In java it is often implemented by a concrete class.
2) Abstract product role: It is generally the parent class inherited by a specific product or the interface implemented. It is implemented in java by interface or abstract class.
3) Specific product role: The object created by the factory class is an instance of this role. Implemented by a concrete class in java.
The factory method pattern removes the static attributes of the factory method in the simple factory pattern, allowing it to be inherited by subclasses. In this way, the pressure concentrated on the factory method in the simple factory pattern can be shared by different factory subclasses in the factory method pattern.
Look at its composition:
1) Abstract factory role: This is the core of the factory method pattern, which has nothing to do with the application. It is the interface that a specific factory role must implement or the parent class that must be inherited. In java it is implemented by abstract classes or interfaces.
2) Specific factory role: It contains code related to specific business logic. Called by an application to create an object corresponding to a specific product.
3) Abstract product role: It is the parent class inherited by a specific product or the interface implemented. In Java, there are generally abstract classes or interfaces to implement.
4) Specific product role: The object created by the specific factory role is an instance of this role. It is implemented by specific classes in java.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/873921.htmlTechArticlephp design pattern - simple factory pattern (static factory method pattern), php design pattern concept simple factory pattern [static factory] Method pattern] (Static Factory Method) is the creation pattern of classes...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn