I know that there are many similar questions, but I don't understand most of those questions because I'm not sure if I know what a factory method pattern is..
so..after reading many examples over the web, I came up with the following simple classes.
Am I doing it correctly?
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();
Yes. That's a correct implementation of the factory method
pattern.
Edit +1 for silent's comment. Should indeed be on coderewiew, didn't thought about that.