Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • September 28, 2009
  • Views: 41,909
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Factory Method.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as Product.
    new product class
  4. Set the Product class 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 ConcreteProduct.
    create concrete product
  6. Create a class 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 ConcreteCreator.
  12. Make ConcreteCreator inherit the abstract operations provided from 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 operations in the Creator class. To represent this, stereotype the Creator class as PTN Members Creatable. Right-click on Creator class and select Stereotypes > Stereotype from the popup menu.
    stereotype creator
  15. In the Stereotypes tab of 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 step 14 and 15 to stereotype it as PTN Members Creatable.
  17. There may be multiple concrete products and creator. Let's repeat step 14 and 15 to stereotype ConcreteProduct and ConcreteCreator as PTN Cloneable. The diagram should look like this:
    pattern modeled

Defining 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 Factory Method. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

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

  1. Create a new project Text Editor
  2. Create a class diagram 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 at the bottom pane.
    rename product
  7. Click on +, and select New Operation... from the popup menu. We shall create the operations available in the TextDocument class.
    new oper
  8. In the Operation Specification dialog box, enter Open as operation name.
    oper name
  9. Repeat step 7 and 8 to create operations close, save, 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 document. Keep ConcreteProduct selected and click the + button, then select Clone... in the popup menu.
    clone
  13. Enter 1 to be the number of classes of clone. Click OK to confirm.
    clone count
  14. Enter RTFDocument as class name.
    name concrete product 2
  15. Select the Creator class in overview.
  16. Rename Creator to TextEditor, operations FactoryMethod to createDoc, 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 name.
    name oper
  19. Repeat the previous steps to create operations closeDoc, saveDoc, reopenDoc.
    creator named
  20. Select ConcreteCreator. Rename ConcreteCreator to MyTextEditor and operation FactoryMethod to createDoc.
    concrete creator done
  21. Click OK to confirm editing and apply the pattern to diagram.
  22. Tidy up the diagram. It should become:
    factory method applied



Turn every software project into a successful one.

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