Thursday, January 3, 2013

The Abstract Factory Pattern

Source: http://java.dzone.com/articles/design-patterns-abstract-factory

The Abstract Factory is known as a creational pattern - 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. 
The AbstractFactory defines the interface that all of the concrete factories will need to implement in order to product Products. ConcreteFactoryA and ConcreteFactoryB have both implemented this interface here, creating two seperate families of product. Meanwhile, AbstractProductA andAbstractProductB are interfaces for the different types of product. Each factory will create one of each of these AbstractProducts. 
The Client deals with AbstractFactory, AbstractProductA and AbstractProductB. It doesn't know anything about the implementations. The actual implementation of AbstractFactory that the Clientuses 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 of ConcreteFactory that implements AbstractFactory, 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 the Clients 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 our AbstractProduct
1.//Our AbstractProduct
2.public interface Window
3.{
4. 
5.public void setTitle(String text);
6. 
7.public void repaint();
8.}
Let's create two implementations of the Window, as our ConcreteProducts. One for Microsoft Windows: 
01.//ConcreteProductA1
02.public class MSWindow implements Window
03.{
04.public void setTitle()
05.{
06.//MS Windows specific behaviour
07.}
08. 
09.public void repaint()
10.{
11.//MS Windows specific behaviour
12.}
13.}
And one for Mac OSX
01.//ConcreteProductA2
02.public class MacOSXWindow implements Window
03.{
04.public void setTitle()
05.{
06.//Mac OSX specific behaviour
07.}
08. 
09.public void repaint()
10.{
11.//Mac OSX specific behaviour
12.}
13.}
Now we need to provide our factories. First we'll define our AbstractFactory. For this example, let's say they just create Windows: 
1.//AbstractFactory
2.public interface AbstractWidgetFactory
3.{
4.public Window createWindow();
5.}
Next we need to provide ConcreteFactory implementations of these factories for our two operating systems. First for MS Windows:
01.//ConcreteFactory1
02.public class MsWindowsWidgetFactory
03.{
04.//create an MSWindow
05.public Window createWindow()
06.{
07.MSWindow window = new MSWindow();  
08.return window;
09.}
10.}
And for MacOSX: 
01.//ConcreteFactory2
02.public class MacOSXWidgetFactory
03.{
04.//create a MacOSXWindow
05.public Window createWindow()
06.{
07.MacOSXWindow window = new MacOSXWindow();  
08.return window;
09.}
10.}
Finally we need a client to take advantage of all this functionality. 
01.//Client
02.public class GUIBuilder
03.{
04.public void buildWindow(AbstractWidgetFactory widgetFactory)
05.{
06.Window window = widgetFactory.createWindow();  
07.window.setTitle("New Window");
08.}
09.}
Of course, we need some way to specify which type of AbstractWidgetFactory to our GUIBuilder. This is usually done with a switch statement similar to the code below:
01.public class Main{
02.public static void main(String[] args)  
03.{
04.GUIBuilder builder = new GUIBuilder();
05.AbstractWidgetFactory widgetFactory = null;
06.//check what platform we're on
07.if(Platform.currentPlatform()=="MACOSX")
08.{
09.widgetFactory  = new MacOSXWidgetFactory();
10.}
11.else
12.{
13.widgetFactory  = new MsWindowsWidgetFactory();
14.}
15.builder.buildWindow(widgetFactory);
16.}
17.}
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.