这是正确的工厂方法模式吗? [英] Is this correct factory method pattern?
本文介绍了这是正确的工厂方法模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道有很多类似的问题,但是我大部分都不理解,因为我不确定我是否知道什么是工厂方法模式.
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.
我做对了吗?
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();
推荐答案
是.这是factory method
模式的正确实现.
Yes. That's a correct implementation of the factory method
pattern.
编辑 +1,以静默发表评论.确实应该在coderewiew上,没想到.
Edit +1 for silent's comment. Should indeed be on coderewiew, didn't thought about that.
这篇关于这是正确的工厂方法模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文