简体   繁体   English

这是正确的工厂方法模式吗?

[英]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. 因此,在阅读了许多网络示例之后,我想到了以下简单的类。

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();

To be exact: This is a Factory pattern , not a Factory method pattern . 确切地说:这是Factory模式 ,而不是Factory方法模式

The difference is that in the Factory pattern you have a separate factory object ( DriveableFactory ), whereas in the Factory method pattern, the create() method would be a member of the Driveable base class. 区别在于,在Factory模式中,您有一个单独的工厂对象( DriveableFactory ),而在Factory方法模式中, create()方法将是Driveable基类的成员。 I'm not familiar with php, and the pattern doesn't seem to be applicable to your concrete scenario anyway, so I cannot give you a code example here. 我对php不熟悉,而且该模式似乎也不适用于您的具体情况,因此在这里我无法给您提供代码示例。

But in any case, you have to distinguish the two patterns. 但是无论如何,您都必须区分这两种模式。 I know that there is a lot of confusion around them, and the Wikipedia entry for Factory method pattern is just plain wrong - among a lot of other sources that can be found on the web. 我知道他们周围有很多困惑,而Wikipedia中关于Factory方法模式的条目完全是错误的-在网上可以找到许多其他资源。 But it's better to be exact on this, because it's essential for communication to mean the same things when you're using the same words... 但是最好对此有所保留,因为当您使用相同的单词时,交流对于表达相同的含义至关重要。

HTH - Thomas HTH-托马斯

Yes. 是。 That's a correct implementation of the factory method pattern. 那是factory method模式的正确实现。

Edit +1 for silent's comment. 编辑 +1以静默发表评论。 Should indeed be on coderewiew, didn't thought about that. 确实应该在coderewiew上,没有考虑过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM