Richard Pérez
Richard Pérez

Reputation: 1487

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?

Upvotes: 0

Views: 57

Answers (2)

Kislay Kishore
Kislay Kishore

Reputation: 154

The need to for abstraction(using interface or abstract class) comes when you need to change the code for creating the concrete objects. This is particularly helpful when situations arises such as while writing unit tests or you need add few more products (or/and) creators.

It is interesting to know that there is difference between factory and factory method. Factories are ways to encapsulate the creation part into a class having a method which takes care of the creation part. On the other hand, Factory Methods are given to "Creators" for creating "Product(s)", which can be decided by the creator. The method to create Product, say createProduct(), is usually inside the Creator, ( induced by using inheritance) and the intention of other methods are usually to work on the product, created by Factory Method.

Now, in both creators and products, we use a parallel sort of hierarchy, so that creator do not depend (or know how the products are created) on Concrete Products and Products also are unaware of creators. This helps in achieving loosely coupled code (here, CHANGE does not impact each other).

Hope this helps.

You can read more at:

Upvotes: 1

Evert
Evert

Reputation: 99687

This is very subjective, but I would argue you don't need any inheritence for it to be a factory method. This would actually suffice to meet the definition:

class Book { 

}

class BookFactory {

   function createBook() {

     return new Book();    

   }

}

Upvotes: 0

Related Questions