php 设计模式之工厂模式

时间:2022-10-01 09:34:56
Interface Transport{
public function go();
}
class Bus implements Transport{
public function go(){
echo "Bus很慢,很拥挤";
}
}
class Car implements Transport{
public function go(){
echo "Car 很快,舒适,干净";
}
}
class Fly implements Transport{
public function go(){
echo "Fly会飞,高大上";
}
}
class gowork{
public static function choice($way){
$ops=null;
switch($way){
case 'Bus':
$ops=new Bus();
break;
case 'Car':
$ops=new Car();
break;
case 'Fly':
$ops=new Fly();
break;
default :
//return '参数错误';
}
return $ops;
}
}
$re=gowork::choice('Fly');
echo $re->go();