我知道有很多类似的问题,但我不明白大多数这些问题,因为我不知道我是否知道工厂方法模式是什么..这是正确的工厂方法模式吗?
so..after read many网络,我想出了以下简单的类。
我做得对吗?
abstract class Driveable
{
abstract public function start();
abstract public function stop();
}
class CoupeDriveable extends Driveable
{
public function start()
{
}
public function stop()
{
}
}
class MotorcycleDriveable extends Driveable
{
public function start()
{
}
public function stop()
{
}
}
class SedanDriveable extends Driveable
{
public function start()
{
}
public function stop()
{
}
}
class DriveableFactory
{
static public function create($numberOfPeople){
if($numberOfPeople == 1)
{
return new MotorcycleDriveable;
}
elseif($numberOfPeople == 2)
{
return new CoupleDriveable;
}
elseif($numberOfPeople >= 3 && $numberOfPeople < 4)
{
return SedanDriveable;
}
}
}
class App
{
static public function getDriveableMachine($numberOfPeople)
{
return DriveableFactory::create($numberOfPeople);
}
}
$DriveableMachine = App::getDriveableMachine(2);
$DriveableMachine->start();
应该在http://codereview.stackexchange .com/ – ariefbayu
@silent //啊...谢谢!!不知道stackexchange有codereview网站。 – Moon
没问题,只是帮助推广其他堆栈交换 – ariefbayu