Jump to Menu

Abstract Factory Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) abstract factory design pattern. By reading this tutorial, you will know how to develop a model for the abstract factory pattern, and how to apply it in practice.

Compatible edition(s): Enterprise, Professional, Standard

  • September 28, 2009
  • Views: 53,869
  • PDF

What is Abstract Factory Design Pattern

The Abstract Factory Design Pattern is a creational design pattern that allows you to create families of related objects without specifying their concrete classes. It provides an interface for creating families of related or dependent objects, without specifying their concrete classes. This pattern provides a way to encapsulate a group of individual factories that have a common theme.

The pattern works by defining an abstract factory interface that specifies a set of methods for creating abstract product objects. Concrete factory classes then implement these methods to create specific product objects. This approach enables you to create families of related objects that are designed to work together, making your code more flexible and easier to maintain.

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Abstract Factory.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as AbstractFactory.
    new abstract class
  4. Select the AbstractFactory abstract by rioght clicking on AbstractFactory and select Model Element Properties > Abstract from the popup menu.
    set abstract class
  5. Create the abstract product classes AbstractProductA and AbstractProductB. Set them as abstract. Up to now, the diagram should look like this:
    create abstract products
  6. Right-click on AbstractFactory and select Add > Operation from the popup menu.
    add oper
  7. Name it as CreateProductA(), and make AbstractProductA as return type.
    create product a added
  8. Add also operation CreateProductB(), and make AbstractProductB as return type.
    create product b added
  9. Set both operations as abstract by right clicking on the operation and selecting Model Element Properties > Abstract from the popup menu.
    set opers abstract
  10. Move the cursor over AbstractProductA, make use of resource icon Generalization -> Class to create two subclasses Product A1 and Product A2.
    sub class for abstract product a
  11. Similarly, create subclasses ProductB1 and ProductB2 from AbstractProductB. Up to now, the 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 on ConcreteFactory2. Up to now, the diagram should look like this:
    opers inherited
  15. Link up 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 on operation CreateProductB. Up to now, the diagram should look like this:
    dependencies
  17. Finally, create the Client class.
    create client class

Defining Pattern

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

Applying Design Pattern on Class Diagram

In this section, we are going to apply the abstract factory pattern in modeling a restaurant system which delivers both Chinese and Western meal sets.

  1. Create a new project Restaurant.
  2. Create a class diagram 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.
    select abstract product a
  6. Rename AbstractProductA to MainCourse at 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 comes the factory branch. Rename AbstractFactory to MealFactory first.
    rename factory
  10. Also rename the operations from CreateProductA and CreateProductB to PrepareMainCourse and Prepare Dessert.
    opers renamed
  11. Similarly, rename ConcreteFactory1 to WesternMealFactory, and rename operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert accordingly.
    concrete factory renamed
  12. Similarly, rename ConcreteFactory2 to ChineseMealFactory, and rename operations CreateProductA and CreateProductB to PrepareMainCourse and PrepareDessert accordingly.
    concrete factory2 renamed
  13. Finally, rename Client to Restaurant.
  14. Click OK to confirm the changes, and apply the pattern. The following diagram is obtained.
    diagram formed



創造美好 共同成長

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK