lundi 7 septembre 2015

Method Factory with Non Abstract Product

I was implementing the Factory Method pattern, but checking several examples out there I couldn't determinate if extending a concrete class for the product instead of creating an abstract class or interface is correct... This is just intended for an example (PHP).

So, I have my abstract factory and their concrete factories:

interface BookFactoryInterface
{
    public function createBook();
}

class ElectronicBookFactory implements BookFactoryInterface
{
    public function createBook()
    {
        return new ElectronicBook();
    }
}

class PaperBookFactory implements BookFactoryInterface
{
    public function createBook()
    {
        return new PaperBook();
    }
}

Now what I have seen in all the examples is that usually products extend from an abstract class or an interface, but when I was testing I realized that in my case there was not need for that, what I was needing is a concrete class with the common behavior and then my subclasses with the rest.

class Book
{
    /* 
      All properties like title, authors, publicationDate
      here, with the corresponding methods.
    */
}

class ElectronicBook extends Book
{
    //...
}

class PaperBook extends Book
{
    //...
}

The class instantiation is still been derived to subclasses, so I really believed this is a Factory Method implementation, but I could find another example code in this way.

So the question: is this still a Factory Method implementation? if not Why?

Aucun commentaire:

Enregistrer un commentaire