简单工厂-有什么意义?

时间:2019-01-09 19:23:04

标签: factory factory-pattern

只想问一下简单的facory模式。

我读了描述这些类和接口的文章:

interface Door
{
    public function getWidth(): float;
    public function getHeight(): float;
}

class WoodenDoor implements Door
{
    protected $width;
    protected $height;

    public function __construct(float $width, float $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function getWidth(): float
    {
        return $this->width;
    }

    public function getHeight(): float
    {
        return $this->height;
    }
}

那里也推了工厂课:

class DoorFactory
{
    public static function makeDoor($width, $height): Door
    {
        return new WoodenDoor($width, $height);
    }
}

有什么不同:

$ door = DoorFactory:makeDoor(100,200);

$ door = new WoodenDoor(100,200); ?

在此示例中,我无法区分差异和意义。

感谢您的明智回答。

0 个答案:

没有答案