Working with Abstract Factory Design Pattern

This tutorial is aimed to guide the definition and application of 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.

September 28, 2009
User Rating: / 9
Views: 11,087
PDF Link Add comments
Edition: Standard or above (Edition comparison)

Modeling

  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

Application

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

Resources

  1. Design Patterns.vpp
  2. Abstract Factory.pat

Rate this Article

Click on one of the stars below to rate this article from 1 (lowest) to 5 (highest).

You may be interested in

  1. Writing Effective Use Case
  2. Draw Entity Relationship Diagram (ERD)
    Tagged:
  3. UML Package Diagram
    Tagged:
  4. Generate Class Diagram from Entity Relationship Diagram (ERD)
    Tagged:
  5. Produce UML Use Cases from Business Process Diagram (BPD)
Tags of this article:

Comments (0)

Write comment

Rating

Comment

Enter the calculation result

security code