Quick Design Pattern Review - Abstract Factory
Need a refresher on Design Patterns? There are plenty of Design Patterns out there. Over the next few months, I plan on writing a bit about most of the common design patterns that can make your life (and mine) as a developer much easier. My aim is to give you just enough of the idea to then be able to research the details from some other resources (some of which I have included).
To start off, I’ll be going through a bunch of Creational Patterns, patterns that are useful when you need to create objects.
To start off, let’s talk about the Abstract Factory pattern. The Abstract Factory pattern enables the creation of a set of common objects without having to know the concrete classes themselves.
In Java terms, this usually boils down to an abstract class. It is used to create a specific interface that all Factories will agree to. By agreeing to an interface up front, any class that uses one of these Factories doesn't need to know anything about what type of Factory it is really talking to.
What It Can Do for You
From the code, this allows you the ability to swap out Factory types with a single line change. All that is needed is to change the Abstract pointer’s concrete Factory instance. Nothing else needs to be changed.
What It Can’t Do for You
There are a few drawbacks with this design. If you find yourself having to add new types (via new methods) to the Abstract Factory class, you will have to add this same method to all concrete Factory classes as well. This effectively means that all Factories must be modified when any additions are needed.
For more information, including examples, check out these resources:
The classic Design Patterns book: Design Patterns: Elements of Reusable Object-Oriented Software
Why use a Abstract Factory? http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern