This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Abstract Factory Pattern?

According to Wikipedia, the Abstract Factory Pattern solves problems like:

  • How can an application be independent of how its objects are created?
  • How can a class be independent of how the objects it requires are created?
  • How can families of related or dependent objects be created?

Creating objects directly within the class that requires the objects is inflexible because it commits the class to particular objects and makes it impossible to change the instantiation later independently from (without having to change) the class. It stops the class from being reusable if other objects are required, and it makes the class hard to test because real objects can’t be replaced with mock objects.

The Abstract Factory design pattern describes how to solve such problems:

  • Encapsulate object creation in a separate (factory) object. That is, define an interface (AbstractFactory) for creating objects, and implement the interface.
  • A class delegates object creation to a factory object instead of creating objects directly.

UML Diagram

The following diagram shows the Factory Method Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

[plantuml]

@startuml
skinparam componentStyle uml2

title Abstract Factory Pattern

class Client
class AbstractFactory <<interface>>
class AbstractProductA <<interface>>
class ProductA2
class ProductA1
class ConcreteFactory1
class ConcreteFactory2
class AbstractProductB <<interface>>
class ProductB1
class ProductB2

AbstractFactory <– Client
Client –> AbstractProductA
Client –> AbstractProductB
AbstractFactory <|.. ConcreteFactory1
AbstractFactory <|.. ConcreteFactory2
AbstractProductA <|.. ProductA1
AbstractProductA <|.. ProductA2
AbstractProductB <|.. ProductB1
AbstractProductB <|.. ProductB2
ProductA1 <– ConcreteFactory1
ProductB1 <– ConcreteFactory1
ProductA2 <– ConcreteFactory2
ProductB2 <– ConcreteFactory2

note right of Client
The Client is written against the
abstract factory and then composed at
runtime with an actual factory.
end note

note left of AbstractFactory
The AbstractFactory defines
the interface that all Concrete
factories must implement which
consists of a set of methods
for producing products.
end note

note “The concrete factories implement the\ndifferent product families. To create a\nproduct the client uses one of these factories,\nso it never has to instantiate a product object.” as n1
ConcreteFactory1 .. n1
ConcreteFactory2 .. n1

note “This is the product\nfamily. Each concrete\nfactory can produce an\nentire set of products.” as n2
AbstractProductA .. n2
AbstractProductB .. n2
@enduml

[/plantuml]

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:

[code language=”javascript”]

@startuml
skinparam componentStyle uml2

title Abstract Factory Pattern

class Client
class AbstractFactory &lt;&lt;interface&gt;&gt;
class AbstractProductA &lt;&lt;interface&gt;&gt;
class ProductA2
class ProductA1
class ConcreteFactory1
class ConcreteFactory2
class AbstractProductB &lt;&lt;interface&gt;&gt;
class ProductB1
class ProductB2

AbstractFactory &lt;– Client
Client –&gt; AbstractProductA
Client –&gt; AbstractProductB
AbstractFactory &lt;|.. ConcreteFactory1
AbstractFactory &lt;|.. ConcreteFactory2
AbstractProductA &lt;|.. ProductA1
AbstractProductA &lt;|.. ProductA2
AbstractProductB &lt;|.. ProductB1
AbstractProductB &lt;|.. ProductB2
ProductA1 &lt;– ConcreteFactory1
ProductB1 &lt;– ConcreteFactory1
ProductA2 &lt;– ConcreteFactory2
ProductB2 &lt;– ConcreteFactory2
@enduml

[/code]