Benefits from factory method pattern

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

Favor 'object composition' over 'class inheritance

And then i saw Factory Method with

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

and subclassing

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 second method we give loose coupling and interchangeable. But Factory Method exists - so someone need it. What are advantages from Factory Method?


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

In particular, your OurClass example occurs to me as a typical use case of the abstract factory pattern. More concretely, the abstract factory pattern gives you the flexibility to make the concrete type of objects being created a parameter of a class and a typical use case of it can be dependency injection (even though this is typically done in a more automated way), see also Dependency Injection vs Factory Pattern.

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

Thus, one simple answer to the question when to use the factory method pattern is the following: When the creation of objects is the subclass's responsibilty. This means, when only the subclass should or can decide what object needs to be created and thus is an aspect of the subclass's behavior and this brings us back full circle to your initial assumption and boils down to the question: When should I use inheritance?, respectively Prefer composition over inheritance?.

The abstract factory pattern on the other hand can be used in the case you describe it, when an external factor decides which objects need to be created and this flexibility is required, thus justifying the additional complexity needed for the code.

链接地址: http://www.djcxy.com/p/77764.html

上一篇: IoC容器的好处

下一篇: 工厂方法模式带来的好处