首页 > 代码库 > 工厂模式

工厂模式

<?php

//工厂模式
abstract class YunSuan
{
public $a;
public $b;

function Suan()
{
}
}


class Jia extends YunSuan
{
function Suan()
{
return $this->a+$this->b;
}
}

class Jian extends YunSuan
{
function Suan()
{
return $this->a-$this->b;
}
}

class Cheng extends YunSuan
{
function Suan()
{
return $this->a*$this->b;
}
}

//做一个工厂类
class GongChang
{
static function ShengChan($fuhao)
{
switch($fuhao)
{
case "+":
return new Jia();
break;
case "-":
return new Jian();
break;
case "*":
return new Cheng();
break;
}
}
}

//算加法
$suan = GongChang::ShengChan("*");
$suan->a = 10;
$suan->b = 5;
echo $suan->Suan();

 

 

?>

工厂模式