Home > php教程 > php手册 > Demonstration factory pattern

Demonstration factory pattern

WBOY
Release: 2016-09-01 00:00:49
Original
1335 people have browsed it
<?<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;">
需求:请设计一个类,该类有一个方法,能够做到这种效果:
  给该方法传入一个类名,则该方法就可以返回这个类的对象。就像一个
&ldquo;塑料制品&rdquo;工厂:给它一个某种产品的&ldquo;模具&rdquo;,就可以生产该模具对
应的塑料制品。
</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 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>
Copy after login

 

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