目标创建对象;
实现 1;实现工厂类 2;调用测试
<?php//调用工厂类require_once "./jiandangongcheng.php"; $po = array(); $po[] = pro::GetInstance(12,1); $po[] = pro::GetInstance(1,11); $po[] = pro::GetInstance(1,1); foreach ($po as $key => $value) { if ($value) { echo "<br/>"; $value->x_caozuo(); echo "<br/>"; $value->Y_caozuo(); }else { echo "非法"; } echo "<br/>"; }
1 <?php 2 //实现工厂类 3 interface product{ 4 function X_caozuo(); 5 function Y_caozuo(); 6 } 7 8 class Xproduct implements product{ 9 10 function X_caozuo(){ 11 echo "我实现了X操作"; 12 } 13 function Y_caozuo() { 14 echo "我没有实现Y_操作 "; 15 } 16 17 } 18 19 class Yproduct implements product { 20 21 function x_caozuo() { 22 echo "类Yproduct 没有实现X操作"; 23 } 24 function Y_caozuo() { 25 echo "类Yproduct 实现Y操作"; 26 } 27 } 28 29 30 class pro{ 31 static function GetInstance($x,$y){ 32 if ($x>1 && $y===1) { 33 return new Xproduct(); 34 }elseif ($x == 1 && $y >1) { 35 return new Yproduct(); 36 } else { 37 return null; 38 } 39 } 40 }