Jump to Menu

Prototype Pattern Tutorial

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

Compatible edition(s): Enterprise, Professional, Standard

  • September 30, 2009
  • Views: 39,821
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Pattern.
  2. Create a class diagram Prototype.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as Client.
    create client class
  4. Right-click on the Client class, and select Add > Operation from the popup menu.
    add oper in client
  5. Name the operation Operation().
    new oper in client
  6. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Prototype.
    new prototype
  7. Right-click on Prototype, and select Model Element Properties > Abstract to set it as abstract.
    set abstract
  8. Add an operation Clone() to Prototype. Make it return Prototype.
  9. Right-click on Clone(), and select Model Element Properties > Abstract to set it as abstract.
    set clone abstract
  10. Move the mouse cursor over the Prototype class, and drag out Generalization > Class to create a subclass ConcretePrototype.
    create concrete prototype
  11. Make ConcretePrototype inherit the abstract operations provided from Prototype by right clicking on ConcretePrototype, and selecting Related Elements > Realize all Interfaces from the popup menu.
    realize concrete prototype
  12. In practice, there may be multiple ConcretePrototype classes. To represent this, stereotype the ConcretePrototype class as PTN Cloneable. Right-click on ConcretePrototype class and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete prototype
  13. In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
    stereotype ptn cloneable
    The diagram should look like this:
    pattern modeled

Defining Pattern

  1. Select all classes on the class diagram.
    select all 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 Prototype. Keep the file name as it. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

In this section, we will try to make use of the prototype pattern to model a part of diagram editor.

  1. Create a new project My Diagram Tool.
  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 design pattern
  4. In the Design Pattern dialog box, select Prototype from the list of patterns.
    select prototype
  5. Click on Client in the overview.
    select client
  6. Rename it to EditTool at the bottom pane.
    rename client
  7. Rename Operation to Duplicate.
    rename operation
  8. Select Prototype in overview.
  9. Rename Prototype to Shape at the bottom pane, and rename the operation Clone to Duplicate.
    rename prototype
  10. Select ConcretePrototype in overview.
  11. Rename ConcretePrototype to OvalShape at the bottom pane, and rename the operation Clone to duplicate.
    rename concrete prototype
  12. We need to have two more concrete prototype classes for square and triangle. Keep ConcretePrototype selecting, click the + button, and select Clone... from the popup menu.
    clone
  13. Enter 2 to be the number of classes to clone.
    clone count
  14. Rename ConcretePrototype2 and ConcretePrototype3 to Square and Triangle respectively.
    Rename the two Clone operations to duplicate.
    rename concrete prototype classes
  15. Click OK to confirm editing and apply the pattern to diagram.
  16. Tidy up the diagram. It should become:
    result



创造美好 共同成长

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