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.

September 28, 2009
Views: 66,307
PDF Download

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

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Abstract Factory.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it AbstractFactory.
    new abstract class
  4. Set the AbstractFactory class as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.
    set abstract class
  5. Create the abstract product classes AbstractProductA and AbstractProductB. Mark them as abstract. At this point, your diagram should resemble this:
    create abstract products
  6. Right-click on AbstractFactory and select Add > Operation from the popup menu.
    add oper
  7. Name the operation CreateProductA() and set AbstractProductA as its return type.
    create product a added
  8. Also, add an operation CreateProductB(), with AbstractProductB as its return type.
    create product b added
  9. Set both operations as abstract by right-clicking on each operation and selecting Model Element Properties > Abstract from the popup menu.
    set opers abstract
  10. Move the cursor over AbstractProductA, then use the resource icon Generalization > Class to create two subclasses, ProductA1 and ProductA2.
    sub class for abstract product a
  11. Similarly, create subclasses ProductB1 and ProductB2 from AbstractProductB. At this point, your diagram should look like this:
    sub class for abstract product b
  12. Create subclasses ConcreteFactory1 and ConcreteFactory2 from AbstractFactory.
    sub class for abstract factory
  13. Inherit operations from AbstractFactory by right-clicking on ConcreteFactory1 and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  14. Repeat step 13 for ConcreteFactory2. Your diagram should now appear as follows:
    opers inherited
  15. 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.
    show dependencies
  16. Repeat step 15 for the operation CreateProductB. Your diagram should now look like this:
    dependencies
  17. Finally, create the Client class.
    create client class

Defining the Pattern

  1. Select all classes on the class diagram.
    select classes
  2. Right-click on the selection and choose Define Design Pattern... from the popup menu.
    define pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Abstract Factory. Keep the file name as is. Click OK to proceed.
    name pattern

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.

  1. Create a new project named Restaurant.
  2. Create a class diagram named Meal Preparation.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply pattern
  4. In the Design Pattern dialog box, select Abstract Factory from the list of patterns.
    select pattern
  5. Click on AbstractProductA in the preview pane.
    select abstract product a
  6. Rename AbstractProductA to MainCourse in the bottom pane.
    rename abstract product a
  7. Similarly, rename ProductA1 to WesternMainCourse and ProductA2 to ChineseMainCourse.
    rename product a1 a2
  8. Rename AbstractProductB, ProductB1, and ProductB2 to Dessert, WesternDessert, and ChineseDessert, respectively.
    rename product b1 b2
  9. Now, address the factory branch. First, rename AbstractFactory to MealFactory.
    rename factory
  10. Also, rename the operations from CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert.
    opers renamed
  11. Similarly, rename ConcreteFactory1 to WesternMealFactory, and update its operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert, respectively.
    concrete factory renamed
  12. Likewise, rename ConcreteFactory2 to ChineseMealFactory, and update its operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert, respectively.
    concrete factory2 renamed
  13. Finally, rename Client to Restaurant.
  14. Click OK to confirm the changes and apply the pattern. The following diagram will be generated:
    diagram formed