1

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.

4

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.

  • Thank you for your answer, but respectfully I think you might be confusing 'factory methods' - methods that instantiate an object according to some parameters and return it to the client - and the Factory Method design pattern, which is named after 'factory methods' because it contains them in the design, but is in fact something different – Aviv Cohn Apr 6 '14 at 0:50
  • @Prog very good point. I extended my explanantion to explicitly mention the factory method pattern, and added direct comparison. – amon Apr 6 '14 at 9:45
  • Thanks for updating. Question: Let's call the class that needs objects, the client. You said that in FMP, we subclasses the client itself, and it differs the concrete instantiation to the subclass. Meaning: If Class A needs objects, but we want to decouple the concrete instantiation from Class A, we could subclass Class A itself, and differ concrete instantiation to the subclass. Now my question: It seems to me that AFP is the exact same system, with only one difference (continuing next comment) – Aviv Cohn Apr 6 '14 at 9:53
  • In AFP, the system is external to the client, and the client is composed with this system to use it. Aka, the client holds an AbstractFactory member reference, leading to some factory it can use. As opposed to the FMP, where the client itself is part of the system. Apart from that, I see no differences. AFP tends to offer several factory-methods to instantiate different concrete objects, but as you said that's not a requirement. – Aviv Cohn Apr 6 '14 at 9:55
  • The question: If so, than this means that if the client needs a factory and we create a FMP system (which the client is part of, as the FMP states), and then later we compose this system with some external object (so the external object will use this system - a client), then to the external object - this is actually an AFP. Do you agree? – Aviv Cohn Apr 6 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.