PHP工厂模式demo

<?php
//工厂模式

interface operstion{
function oper($a,$b);
}
//加
class add implements operstion{
function oper($a,$b){
echo $c = $a + $b;
}
}
//减
class subtract implements operstion{
function oper($a,$b){
echo $d = $b - $a;
}
}
//乘
class multiply implements operstion{
function oper($a,$b){
echo $e = $a * $b;
}
}
//除
class divide implements operstion{
function oper($a,$b){
$f = $b / $a;
}
}
class Factory{
static public function createDb($type){
switch($type){
case 'add':
return new add();
break;
case 'subtract':
return new subtract();
break;
case 'multiply':
return new multiply();
break;
case 'divide':
return new divide();
break;
}
}
}

$obj = Factory::createDb('add');
$obj->oper(2,4);
posted @ 2018-10-09 14:09  酸suan  阅读(282)  评论(0编辑  收藏  举报