Home > php教程 > php手册 > php设计模式 — 简单工厂模式(静态工厂方法模式),php设计模式

php设计模式 — 简单工厂模式(静态工厂方法模式),php设计模式

WBOY
Release: 2016-06-13 09:25:56
Original
1023 people have browsed it

php设计模式 — 简单工厂模式(静态工厂方法模式),php设计模式

概念

简单工厂模式 【静态工厂方法模式】(Static Factory Method)
是类的创建模式

工厂模式的几种形态:
  1、简单工厂模式(Simple Factory) |又叫做  静态工厂方法模式(Static Factory Method)
  2、工厂方法模式(Factory Method) |又叫做 多态性工厂模式(Polymorphic Factory)
  3、抽象工厂模式(Abstract Factory) |又叫做 工具箱模式(ToolKit)

配图

代码实例

直接将代码运行即可,都是测试过的

<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> }
Copy after login

 

php设计模式: 写出工厂模式与单态模式的PHP5示例代码

Example #1 调用工厂方法(带参数)

class Example
{
// The parameterized factory method
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
------------------------------------
Example #2 单例模式

class Example
{
// 保存类实例在此属性中
private static $instance;

// 构造方法声明为private,防止直接创建对象
private function __construct()
{
echo 'I am constructed';
}

// singleton 方法
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}

return self::$instance;
}

// Example类中的普通方法
public function bark()
{
echo 'Woof!';
}

// 阻止用户复制对象实例
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}

}

?>
 

简单工厂模式与工厂方法模式的相同点与不同点?

建议LZ把简单工厂,工厂方法和抽象工厂一起研究下,呵呵,前一段我老师让我研究下这3种模式的不同,一起学习可能更明白了。
简单工厂模式又称静态工厂方法模式。重命名上就可以看出这个模式一定很简单。它存在的目的很简单:定义一个用于创建对象的接口。
先来看看它的组成:
1)工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑。在java中它往往由一个具体类实现。
2)抽象产品角色:它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。
3)具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。
工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。
看下它的组成:
1)抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。在java中它由抽象类或者接口来实现。
2) 具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
3)抽象产品角色:它是具体产品继承的父类或者是实现的接口。在java中一般有抽象类或者接口来实现。
4)具体产品角色:具体工厂角色所创建的对象就是此角色的实例。在java中由具体的类来实现。
 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template