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 Syntax:</p><p>@startuml<br /> skinparam componentStyle uml2</p><p>title Abstract Factory Pattern</p><p>class Client<br /> class AbstractFactory <<interface>><br /> class AbstractProductA <<interface>><br /> class ProductA2<br /> class ProductA1<br /> class ConcreteFactory1<br /> class ConcreteFactory2<br /> class AbstractProductB <<interface>><br /> class ProductB1<br /> class ProductB2</p><p>AbstractFactory <– Client<br /> Client –> AbstractProductA<br /> Client –> AbstractProductB<br /> AbstractFactory <|.. ConcreteFactory1<br /> AbstractFactory <|.. ConcreteFactory2<br /> AbstractProductA <|.. ProductA1<br /> AbstractProductA <|.. ProductA2<br /> AbstractProductB <|.. ProductB1<br /> AbstractProductB <|.. ProductB2<br /> ProductA1 <– ConcreteFactory1<br /> ProductB1 <– ConcreteFactory1<br /> ProductA2 <– ConcreteFactory2<br /> ProductB2 <– ConcreteFactory2</p><p>note right of Client<br /> The Client is written against the<br /> abstract factory and then composed at<br /> runtime with an actual factory.<br /> end note</p><p>note left of AbstractFactory<br /> The AbstractFactory defines<br /> the interface that all Concrete<br /> factories must implement which<br /> consists of a set of methods<br /> for producing products.<br /> end note</p><p>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<br /> ConcreteFactory1 .. n1<br /> ConcreteFactory2 .. n1</p><p>note “This is the product\nfamily. Each concrete\nfactory can produce an\nentire set of products.” as n2<br /> AbstractProductA .. n2<br /> AbstractProductB .. n2<br /> @enduml</p><p>

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:

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