php设计模式(工厂模式)

<meta charset="utf-8">
<?php
abstract class Operation {
    abstract public function getResult($num1,$num2);
}
class Add extends Operation{
   public function getResult($num1,$num2) {
        $result=$num1+$num2;
        return $result;
    }
}
class Sub extends Operation{
     public function getResult($num1,$num2) {
        $result=$num1-$num2;
        return $result;
    }
}
class Mul extends Operation{
      public function getResult($num1,$num2) {
        $result=$num1*$num2;
        return $result;
    }
}
class Div extends Operation{
    function getResult($num1,$num2) {
        if($num2==0){
            echo"除数不能为0";
            return;
        }else{
             $result=$num1/$num2;
             return $result;
        }
    }
}
interface  Factory{
    function createFactory();
}
class AddFactory implements Factory{
     function createFactory(){
         return new Add();
     }
}
class SubFactory implements Factory{
     function createFactory(){
         return new Sub();
     }
}
class MulFactory implements Factory{
     function createFactory(){
         return new Mul();
     }
}
class DivFactory implements Factory{
     function createFactory(){
         return new Div();
     }
}

class Customer {  
     function  getComputer($type){       
         $class = new ReflectionClass($type );   
         $instance  = $class->newInstanceArgs();  
         return $instance->createFactory();  
    }  
  }

    原文作者:daisy_oo
    原文地址: https://blog.csdn.net/qq_27597973/article/details/52719157
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞