Factory method advantages

I am currently reading Head First Design Patterns and I have a question. At the beginning of the book, I saw this principle:

Enjoy object composition over class inheritance

And then I saw Factory Method with

abstract class Creator{
   abstract Product create(String type);
}

      

and subclasses

class ConcreteCreator extends Creator{
    Product create(String type){
     //creation
    }
}

      

But we can compose our class with Simple Factory like

class OurClass{
     SimpleFactory factory;
     void ourMethod(){
        Product product = factory.create(String type);
     }
     void setFactory(SimpleFactory factory){
        this.factory = factory;
     }

interface SimpleFactory {
     Product create(String type);
}

class AnotherConcreteCreator implements SimpleFactory {
     Product create(String type){
      //creation
     }
}

      

From the second method, we give loose coupling and are interchangeable. But the Factory method exists, so someone needs it. What are the benefits of the Factory Method?

+3


source to share


1 answer


First of all, it is important to distinguish between a factory method pattern and an abstract factory pattern , for that see Differences between abstract factory Pattern and factory Method and Why are there two separate patterns: Abstract factory and factory Method and What is the main difference between factory and Abstract factory Patterns? ... As you can see, this is a little tricky, and therefore what I say in the following should be taken with a grain of salt.

In particular, your example OurClass

comes across to me as a typical example of using an abstract factory pattern. More specifically, the abstract factory pattern gives you the flexibility to create a specific type of object created by a class parameter, and its typical use would be dependency injection (although this is usually done in a more automated way), see also Dependency Injection vs. Factory Pattern .

Roughly speaking, in an abstract factory pattern, you delegate the construction of objects to an external class (and usually this class is responsible for creating not only one, but several related objects), whereas in a factory method pattern, you delegate the construction of objects to subclasses. You can also assume that the abstract factory pattern uses the factory method pattern for its creation methods.



Thus, one simple answer to the question of when to use the factory method pattern is: When object creation is a responsible subclass. This means that when only the subclass has to or can decide which object to create and thus is an aspect of the subclass's behavior, and that brings us back to full circle to your original guess and comes down to the question: when should inheritance be used ?, respectively Prefer composition over inheritance? ...

On the other hand, an abstract factory pattern can be used if you describe it when an external factor decides what objects need to be created and this flexibility is required, thus justifying the additional complexity required for the code.

+4


source







All Articles