Abstract Factory Pattern Tutorial
This tutorial provides a comprehensive guide to understanding and applying the Gang of Four (GoF) Abstract Factory design pattern. You will learn to develop robust models for the Abstract Factory pattern and gain practical insights into its real-world implementation using Visual Paradigm, a leading UML modeling tool.
What is the Abstract Factory Design Pattern?
The Abstract Factory Design Pattern is a creational design pattern that enables the creation of families of related objects without explicitly specifying their concrete classes. It provides an interface for generating families of interconnected or dependent objects, abstracting away their specific implementations. This pattern effectively encapsulates a group of individual factories sharing a common theme.
The pattern operates by defining an abstract factory interface, which declares a set of methods for creating abstract product objects. Concrete factory classes then implement these methods to produce specific product objects. This approach facilitates the creation of cohesive families of objects designed to work together, thereby enhancing code flexibility and maintainability.
Modeling the Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Abstract Factory.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it AbstractFactory.

- Set the AbstractFactory class as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.

- Create the abstract product classes AbstractProductA and AbstractProductB. Mark them as abstract. At this point, your diagram should resemble this:

- Right-click on AbstractFactory and select Add > Operation from the popup menu.

- Name the operation CreateProductA() and set AbstractProductA as its return type.

- Also, add an operation CreateProductB(), with AbstractProductB as its return type.

- Set both operations as abstract by right-clicking on each operation and selecting Model Element Properties > Abstract from the popup menu.

- Move the cursor over AbstractProductA, then use the resource icon Generalization > Class to create two subclasses, ProductA1 and ProductA2.

- Similarly, create subclasses ProductB1 and ProductB2 from AbstractProductB. At this point, your diagram should look like this:

- Create subclasses ConcreteFactory1 and ConcreteFactory2 from AbstractFactory.

- Inherit operations from AbstractFactory by right-clicking on ConcreteFactory1 and selecting Related Elements > Realize all Interfaces from the popup menu.

- Repeat step 13 for ConcreteFactory2. Your diagram should now appear as follows:

- Link the Factory and Product hierarchies by visualizing their dependencies. Right-click on AbstractFactory's operation CreateProductA and select Show Dependencies from the popup menu.

- Repeat step 15 for the operation CreateProductB. Your diagram should now look like this:

- Finally, create the Client class.

Defining the Pattern
- Select all classes on the class diagram.

- Right-click on the selection and choose Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Abstract Factory. Keep the file name as is. Click OK to proceed.

Applying the Design Pattern to a Class Diagram
In this section, we will apply the Abstract Factory pattern to model a restaurant system that delivers both Chinese and Western meal sets.
- Create a new project named Restaurant.
- Create a class diagram named Meal Preparation.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Abstract Factory from the list of patterns.

- Click on AbstractProductA in the preview pane.

- Rename AbstractProductA to MainCourse in the bottom pane.

- Similarly, rename ProductA1 to WesternMainCourse and ProductA2 to ChineseMainCourse.

- Rename AbstractProductB, ProductB1, and ProductB2 to Dessert, WesternDessert, and ChineseDessert, respectively.

- Now, address the factory branch. First, rename AbstractFactory to MealFactory.

- Also, rename the operations from CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert.

- Similarly, rename ConcreteFactory1 to WesternMealFactory, and update its operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert, respectively.

- Likewise, rename ConcreteFactory2 to ChineseMealFactory, and update its operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert, respectively.

- Finally, rename Client to Restaurant.
- Click OK to confirm the changes and apply the pattern. The following diagram will be generated:
