1

An abstract factory typically constructs a "family" of related objects, each sub-classing a respective abstraction. The example here can construct plants and plant pickers; the OrangeFactory constructs Oranges and OrangePickers, and the AppleFactory constructs Apples and ApplePickers.

But if the family only consists of a single abstraction, would it still be an abstract factory? Consider:

interface PlantFactory {
  Plant makePlant();
}

public class AppleFactory implements PlantFactory {
  Plant makePlant() {
    return new Apple();
  }
}

Is this still an example of an abstract factory, or is it something else? I've never seen anything like this in any examples of factories, abstract factories, or factory methods. And yet, it still seems to be useful. Is there anything inherently wrong with such a design pattern?

Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
  • There is a common misconception about the Abstract Factory pattern, which has been propagated for years by people who have not read or understood the GoF book. The misconception is based on some ridiculous idea about a "factory of factories". Nothing could be further from the truth. The [purpose](https://stackoverflow.com/questions/4209791#38668246) of an Abstract Factory is to enforce a common theme across multiple products. Of course you can write code any way you like; and if the example here helps you, that's great. It's just not an Abstract Factory. – jaco0646 Sep 08 '22 at 16:27
  • @jaco0646 I'm not confused what an abstract factory is in general. I'm just asking if the given example satisfies the definition of an abstract factory, which is usually described colloquially and ambiguously. It doesn't make any sense that it wouldn't be an abstract factory; it still constructs a family of related objects subclassing respective abstractions. The family is simply of size 1, in this case. Guru Stron's answer below supports this, including the referenced class diagram. Are you disagreeing with this answer? If so, could you cite a source? – Alexander Guyer Sep 08 '22 at 23:20

1 Answers1

2

Yes. The main goal of abstract factory is basically to abstract over family of factories, how many different object types those factories create is not a major factor in this case.

As example (as always simplified) the diagram from Abstract factory pattern Wiki article can be used:

enter image description here

Guru Stron
  • 102,774
  • 10
  • 95
  • 132