samedi 22 juillet 2017

Abstract factory pattern: why we need Abstract Product Interface

We know that, following classes are needed to design Abstract factory pattern:

  • AbstractFactory

  • ConcreteFactory

  • AbstractProduct

  • Product

My question is why we need AbstractProduct ? Look the following code where i created Abstract factory pattern without any AbstractProduct:

<?php

abstract class AbstractFactory
{
    abstract public function createProduct(string $content);
}

class Factory extends AbstractFactory
{
    public function createProduct(string $content)
    {
        return new Product($content);
    }
}

class Product
{
    private $text;

    public function __construct(string $text)
    {
        $this->text = $text;
    }

    public function getText(){
        return $this->text;
    }
}


$factory = new Factory();
$text = $factory->createProduct('foobar');
echo $text->getText();
?>

Aucun commentaire:

Enregistrer un commentaire