The Abstract Factory is known as acreationalpattern - it's used to construct objects such that they can be decoupled from the implementing system. The definition of Abstract Factory provided in the original Gang of Four book on Design Patterns states:
Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Now, let's take a look at the diagram definition of the Abstract Factory pattern.
Although the concept is fairly simple, there's a lot going on in this diagram. I've used red to note the different between what ConcreteFactory2 is responsible for.
TheAbstractFactorydefines the interface that all of the concrete factories will need to implement in order to productProducts.ConcreteFactoryAandConcreteFactoryBhave both implemented this interface here, creating two seperate families of product. Meanwhile,AbstractProductAandAbstractProductBare interfaces for the different types of product. Each factory will create one of each of these AbstractProducts.
TheClientdeals withAbstractFactory,AbstractProductAandAbstractProductB. It doesn't know anything about the implementations. The actual implementation ofAbstractFactorythat theClientuses is determined at runtime.
As you can see, one of the main benefits of this pattern is that the client is totally decoupled from the concrete products. Also, new product families can be easily added into the system, by just adding in a new type ofConcreteFactorythat implementsAbstractFactory, and creating the specific Product implementations.
For completeness, let's model the Clients interactions in a sequence diagram:
While the class diagram looked a bit busy, the sequence diagram shows how simple this pattern is from theClients point of view. The client has no need to worry about what implementations are lying behind the interfaces, protecting them from change further down the line.
Where Would I Use This Pattern?
The pattern is best utilised when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details. As you'll have noticed, a key characteristic is that the pattern will decouple the concrete classes from the client.
An example of an Abstract Factory in use could be UI toolkits. Across Windows, Mac and Linux, UI composites such as windows, buttons and textfields are all provided in a widget API like SWT. However, the implementation of these widgets vary across platforms. You could write a platform independent client thanks to the Abstract Factory implementation.
So How Does It Work In Java?
Let's take the UI toolkit concept on to our Java code example. We'll create a client application that needs to create a window.
First, we'll need to create our Window interface. Window is ourAbstractProduct.
Just to give a clear idea of how this implementation relates to the Abstract Factory pattern, here's a class diagram representing what we've just done:
Watch Out for the Downsides
While the pattern does a great job of hiding implementation details from the client, there is always a chance that the underlying system will need to change. We may have new attributes to our AbstractProduct, or AbstractFactory, which would mean a change to the interface that the client was relying on, thus breaking the API.
With both the Factory Method and today's pattern, the Abstract Factory, there's one thing that annoys me - someone has to determine what type of factory the client is dealing with at runtime. As you see above, this is usually done with some type of switch statement.