Search code examples
design-patternsumlclass-diagramabstract-factory

Abstract_factory UML diagram in Wikipedia incorrect?


Referring to the UML diagram depicted in https://en.wikipedia.org/wiki/Abstract_factory_pattern#/media/File:Abstract_factory_UML.svg, one could conclude that ConcreteFactory1 and ConcreteFactory2 are derived from AbstractFactory.

enter image description here

But AbstractFactory is an interface, therefore it must be 'implemented', not 'extended'.

What am I missing?


Solution

  • You are correct: The concrete factories should realize (implement) the interface and not specialize it.

    Part of the confusion comes from GoF which was written in a time where only rare languages provided for "interfaces":

    • In the book they uses abstract classes, and when they referred to an interface, it was the implicit interface provided by a class.

    • The transformation of their class diagram in view of a strong separation between classes and interfaces leaves room for interpretation:

      • On oodesign.com they have made an analysis on design patterns from a java perspective, but whereas they've introduced interfaces in a number of cases, they have kept the abstract class for the abstract factory. This makes sense if you consider that abstract factories should easily be specialized further by adapting some of the object types to be generated.

      • On refactoring.guru and other sources, they have adopted the idea of the interface rather than the abstract class for the same pattern. The main argument for this interpretation is the purpose of the pattern, which is to "provide an interface for creating families or related or dependent objects without specifying their concrete types"

    To add to the confusion, obsolete versions of UML (e.g. UML 1.4, section 3.29.1) gave to interfaces the same semantic as purely abstract classes, providing tolerance for such a mixed notation. But this is no longer the case (and for almost two decades). The current UML definition makes an interface a contract that cannot be inherited by a class but must be implemented/realized instead.

    One simple advice: if wikipedia is wrong, cross check carefully, look for some authoritative sources and correct it :-). In this case you could even refer to alignment with the diagrams in the section "Structure".