首页 >php教程 >php手册 >演示工厂模式

演示工厂模式

WBOY
WBOY原创
2016-09-01 00:00:491330浏览
<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">演示工厂模式</span><span style="color: #008000;">
/*</span><span style="color: #008000;">
需求:请设计一个类,该类有一个方法,能够做到这种效果:
  给该方法传入一个类名,则该方法就可以返回这个类的对象。就像一个
“塑料制品”工厂:给它一个某种产品的“模具”,就可以生产该模具对
应的塑料制品。
</span><span style="color: #008000;">*/</span>
 
 <span style="color: #008000;">//</span><span style="color: #008000;">工厂类</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Factory{
 </span><span style="color: #0000ff;">static</span>  <span style="color: #0000ff;">function</span> GetObjectByClassName(<span style="color: #800080;">$class_name</span><span style="color: #000000;">){
     </span><span style="color: #800080;">$obj1</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$class_name</span><span style="color: #000000;">();
     </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj1</span><span style="color: #000000;">;
  }
}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> A{};
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> B{};
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> C{};
</span><span style="color: #008000;">//</span><span style="color: #008000;">$F=new Factory();
//$obj1= $F->GetObjectByClassName("A");</span>
<span style="color: #800080;">$obj1</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">);
</span><span style="color: #800080;">$obj2</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">);
</span><span style="color: #800080;">$obj3</span>= Factory::GetObjectByClassName("B"<span style="color: #000000;">);
</span><span style="color: #800080;">$obj4</span>= Factory::GetObjectByClassName("C"<span style="color: #000000;">);
</span><span style="color: #800080;">$obj5</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj1</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">echo</span> "<br>";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj2</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">echo</span> "<br>";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj3</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">echo</span> "<br>";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj4</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">echo</span> "<br>";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj5</span><span style="color: #000000;">); 

</span><span style="color: #008000;">/*</span><span style="color: #008000;">

object(A)#2 (0) { } 
object(A)#3 (0) { } 
object(B)#4 (0) { } 
object(C)#5 (0) { } 
object(A)#6 (0) { }

</span><span style="color: #008000;">*/</span>

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn