Enterprise Integration Zone is brought to you in partnership with:

Isaac Taylor is an aspiring mobile developer and a technology geek. He spends most of his development time finding kinks in developing apps and mobile web sites for Android, iOS and Windows Phone 7, and he posts the solutions so you don't have to. When not developing software himself, he's likely reading about how to write better code. Isaac is a DZone MVB and is not an employee of DZone and has posted 11 posts at DZone. You can read more from them at their website. View Full User Profile

Quick Design Pattern Review - Abstract Factory

  • submit to reddit

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.

Creational Patterns

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.

Abstract Factory

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:

Published at DZone with permission of Isaac Taylor, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)