//设计模式:工厂模式
/*
class Yunsuan
{
public $a;
public $b;
//public $f;
function jia()
{
return $this->a + $this->b;
}
function jian()
{
return $this->a - $this->b;
}
function cheng()
{
return $this->a * $this->b;
}
function chu()
{
return $this->a / $this->b;
}
}
$y=new Yunsuan();
$y->a=10;
$y->b=5;
echo $y->jia();
*/
//造父类,使用子类继承
/*
class Yunsuan
{
public $a;
public $b;
function Yunsuan()
{
}
}
//加法的子类
class Jia extends Yunsuan
{
function Yunsuan()
{
return($this->a + $this->b);
}
}
//减法的子类
class Jian extends Yunsuan
{
function Yunsuan()
{
return($this->a-$this->b);
}
}
$t=new Jia();
$t->a=10;
$t->b=4;
echo $t->yunsuan();
*/
//再优化,工厂类
class Yunsuan
{
public $a;
public $b;
function Yunsuan()
{
}
}
//加法的子类
class Jia extends Yunsuan
{
function Yunsuan()
{
return($this->a + $this->b);
}
}
//减法的子类
class Jian extends Yunsuan
{
function Yunsuan()
{
return($this->a-$this->b);
}
}
//乘法的子类
class Cheng extends Yunsuan
{
function Yunsuan()
{
return($this->a * $this->b);
}
}
class Gongchang
{
static function DUixiang($f)
{
switch($f)
{
case"+";
return new Jia();
break;
case"-";
return new Jian();
break;
case"*";
return new Cheng();
break;
}
}
}
$r=Gongchang::DUixiang("+");
$r->a=10;
$r->b=5;
echo $r->Yunsuan();