Object-Oriented Design – Is Abstract Factory Pattern a Case of Polymorphism?

design-patternsobject-orientedpolymorphism

I was looking for a pattern/solution that allows me call a method as a runtime exception in a group of different methods without using Reflection. I've recently become aware of the Abstract Factory Pattern.

To me, it looks so much like polymorphism, and I thought it could be a case of polymorphism but without the super class GUIFactory, as you can see in the example of the link above. Am I correct in this assumption?

Best Answer

I guess it depends on how it is used.

Essentially the Factory pattern is a reference to a set of objects. Normally combined with something else, possibly like the Strategy pattern (which is more likely to be defined as a type of polymorphism) to provide reference to an object to act on.

The Abstract Factory Pattern by itself is intended to be polymorphic as it is defined as an abstract class type. However the concrete implementation of the factory is the WidgetFactory in your type and is only polymorphic in reference to using a factory and providing an implementation of a factory.

In terms of what you are after, you certainly require a concrete factory, and presumably the actions you perform depend on the exception being caught. To that end you would use your factory implementation in a non-polymorphic way simply by passing it the exception caught, and have the factory pattern return a method to invoke a strategy or even a chain-of-command pattern to deal with how you would like to handle the exception.

Therefore, your factory would not necessarily be polymorphic, and your exception handlers would be polymorphic.