Factory Method Pattern Tutorial

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

September 28, 2009
Views: 60,926
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Factory Method.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class. Name it Product.
    new product class
  4. Set the Product class as abstract by right-clicking on it and selecting Model Element Properties > Abstract from the popup menu.
    set product abstract
  5. Move the mouse cursor over the Product class and drag out Generalization > Class to create a subclass named ConcreteProduct.
    create concrete product
  6. Create a class named Creator and set it as abstract.
    abstract creator
  7. Right-click on the Creator class and select Add > Operation from the popup menu.
    add oper
  8. Name the operation FactoryMethod() and make it return Product.
  9. Right-click on FactoryMethod() and select Model Element Properties > Abstract to set it as abstract.
    abstract factory method
  10. Add a non-abstract operation AnOperation() to Creator.
    create non abstract method
  11. Move the mouse cursor over the Creator class and drag out Generalization > Class to create a subclass named ConcreteCreator.
  12. Make ConcreteCreator inherit the abstract operations provided by Creator by right-clicking on ConcreteCreator and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  13. In practice, the FactoryMethod in ConcreteCreator is expected to return an instance of ConcreteProduct. Therefore, add a dependency between ConcreteCreator and ConcreteProduct. Move the mouse cursor over the ConcreteCreator class and drag out Dependency > Class to ConcreteProduct. Up to now, the diagram should look like this:
    pattern modeled
  14. There may be more than one operation in the Creator class. To represent this, stereotype the Creator class as PTN Members Creatable. Right-click on the Creator class and select Stereotypes > Stereotype from the popup menu.
    stereotype creator
  15. In the Stereotypes tab of the class specification, select PTN Members Creatable and click > to assign it to the class. Click OK to confirm.
    class stereotypes
  16. The Product class should also have its own operations. Repeat steps 14 and 15 to stereotype it as PTN Members Creatable.
  17. There may be multiple concrete products and creators. Let's repeat steps 14 and 15 to stereotype ConcreteProduct and ConcreteCreator as PTN Cloneable. The diagram should look like this:
    pattern modeled

Defining a Pattern

  1. Select all classes on the class diagram.
    classes selected
  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 as Factory Method. Keep the file name as is. Click OK to proceed.
    name pattern

Applying a Design Pattern to a Class Diagram

In this section, we will try to use the factory method pattern to model a part of a text editor.

  1. Create a new project named Text Editor.
  2. Create a class diagram named Domain Model.
  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 Factory Method from the list of patterns.
    select factory method
  5. Click on Product in the overview.
    select product
  6. Rename it to TextDocument in the bottom pane.
    rename product
  7. Click on +, and select New Operation... from the popup menu. We will create the operations available in the TextDocument class.
    new oper
  8. In the Operation Specification dialog box, enter Open as the operation name.
    oper name
  9. Repeat steps 7 and 8 to create operations close, save, and reopen.
    product opers
  10. Click on ConcreteProduct in the overview.
  11. Rename ConcreteProduct to PlainTextDocument.
    rename concrete product
  12. We need to process one more document type for RTF documents. Keep ConcreteProduct selected and click the + button, then select Clone... from the popup menu.
    clone
  13. Enter 1 as the number of classes to clone. Click OK to confirm.
    clone count
  14. Enter RTFDocument as the class name.
    name concrete product 2
  15. Select the Creator class in the overview.
  16. Rename Creator to TextEditor, the operation FactoryMethod to createDoc, and AnOperation to newDoc.
    rename creator
  17. We need more operations. Click on +, then select New Operation... from the popup menu.
    create oper
  18. In the Operation Specification dialog box, enter loadDoc as the name.
    name oper
  19. Repeat the previous steps to create operations closeDoc, saveDoc, and reopenDoc.
    creator named
  20. Select ConcreteCreator. Rename ConcreteCreator to MyTextEditor and the operation FactoryMethod to createDoc.
    concrete creator done
  21. Click OK to confirm editing and apply the pattern to the diagram.
  22. Tidy up the diagram. It should look like this:
    factory method applied