<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();
}
}
php设计模式(工厂模式)
最新推荐文章于 2022-10-24 01:47:19 发布