我知道有很多類似的問題,但我不明白大多數這些問題,因爲我不知道我是否知道工廠方法模式是什麼..這是正確的工廠方法模式嗎?
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