design-patternsabstract-factory

Is an abstract factory with only one method still an abstract factory?


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?


Solution

  • 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