Pergunta

I've been confused for a while about the differences between the patterns Factory Method and Abstract Factory. Been doing a lot of research, still confused.

I have one question:

Is the only difference between the two patterns, is that Factory Method produces one object, and Abstract Factory produces a family of objects? Or are there more differences?

I read a lot on this site and the web about this, only confused me more. I know how to use the patterns, but am having trouble differentiating between the two.

Foi útil?

Solução

A factory method is essentially just a constructor with another name. This can be useful e.g. to guarantee correct initialization, registration, or to supply default values without having to subclass the product:

private List<WeakReference<Product>> products = ...;

public Product makeProduct(Param x) {
    Product instance = new Product(x, "default value");
    instance.initialize(42);
    products.add(new WeakReference<>(instance));
    return instance;
}

An abstract factory pattern is usually implemented in terms of factory methods, but here the aim is to leave the concrete type unspecified until runtime. So we have a set of products:

interface Product {}
class ConcreteProduct1 implements Product {}
class ConcreteProduct2 implements Product {}

And we have an abstract factory:

interface Factory {
    public Product makeProduct();
}

Such a factory might be used by client code: factory.makeProduct(). Which concrete product is used is delayed to runtime.

class ConcreteFactory1 implements Factory {
    public Product makeProduct() { return new ConcreteProduct1(); }
}

class ConcreteFactory2 implements Factory {
    public Product makeProduct() { return new ConcreteProduct2(); }
}

Typically, an abstract factory will have multiple factory methods for a set of related types, you can think of a factory instance as a “theme”. Abstract factories are also useful for dependency injection.

The factory method pattern is a combination of factory methods with the strategy pattern. Here the idea is that our class needs to create some instance, but will let subclasses override the choice of the instance. In other words, the client and the factory are the same.

class DomainSpecificStuff {
    public doDomainSpecificStuff() {
        Product p = this.makeProduct();
        p.frobnicate();
    }

    protected makeProduct() {
        return new  ConcreteProduct1();
    }
}

class OtherDomainSpecificStuff {
    @Override
    protected makeProduct() {
        return new ConcreteProduct2();
    }
}

So the strategy pattern allows for limited dependency injection through the use of inheritance when used as the FMP.

So what are the useful distinctions between the factory method pattern and abstract factory pattern?

  • Client: The client of the factory method in the FMP is the factory itself – it's just a spin on the strategy pattern, after all. The client of the AFP is some other class.

  • Number of Objects: Typically, the FMP will only have a single factory method, whereas the AFP can be used to produce multiple related objects. It is not the case that an AFP must have at least two factory methods, although the creation of related, interdependent objects is where the AFP shines.

  • Intent: The AFP is a collection of related factory methods which can be passed around in client code. The FMP allows subclasses to swap out a dependency.

Licenciado em: CC-BY-SA com atribuição
scroll top