Visual Paradigm logo
Jump to Menu

Iterator Pattern Tutorial

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

Compatible edition(s): Enterprise, Professional, Standard

  • October 16, 2009
  • Views: 8,126
  • PDF

Modeling

  1. Create a new project Design Patterns.
  2. Create a class diagram Iterator.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Aggregate.
    new aggregate class
  4. Right click on Aggregate, and select Model Element Properties > Abstract to set it as abstract.
    set class abstract
  5. Right click on the Aggregate class, and select Add > Operation from the popup menu.
    add oper to aggregate
  6. Name the operation CreateIterator(), and make it return Iterator.
    name createiter
  7. Right click on CreateIterator() : Iterator, and select Model Element Properties > Abstract to set it as abstract.
    set oper abstract
  8. Move the mouse cursor over the Aggregate class, and drag out Generalization > Class to create a subclass ConcreteAggregate.
    create concrete aggregate
  9. ConcreteAggregate will inherit the operations from Aggregate. Select ConcreteAggregate, right click on them and select Related Elements > Realize all Interfaces from the popup menu.
    realize concrete aggregate
  10. Create a class Client near Aggregate.
    create client class
  11. Move the mouse cursor over the Client class, and drag out Association > Class to Aggregate.
    associate client aggregate
  12. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Iterator.
    create iterator
  13. Right click on Iterator, and select Model Element Properties > Abstract to set it as abstract.
  14. Right click on the Iterator class, and select Add > Operation from the popup menu to add operations: First(), Next(), IsDone(), CurrentItem().
    create opers in ter
  15. Select all operations in Iterator. Right click and select Model Element Properties > Abstract to set it as abstract.
    set opers abstract
  16. ConcreteIterator will inherit the operations from Iterator. Select ConcreteIterator, right click on them and select Related Elements > Realize all Interfaces from the popup menu.
    realize concrete
  17. Move the mouse cursor over the ConcreteAggregate class, and drag out Dependency > Class to class ConcreteIterator.
    add dependency
  18. Move the mouse cursor over the ConcreteIterator class, and drag out Association > Class to class ConcreteAggregate.
  19. In practice, there may be multiple ConcreteAggregate classes. To represent this, stereotype the ConcreteAggregate class as PTN Cloneable. Right click on ConcreteAggregate class and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete aggregate
  20. In the Stereotypes tab of class specification, select PTN Cloneable and click > to assign it to the class. Click OK to confirm.
    select ptn cloneable
  21. There may be multiple ConcreteIterator classes. Right click on ConcreteIterator class and select Stereotypes > PTN Cloneable from the popup menu.
    stereotype concrete iter
    The diagram become:
    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 Iterator. Keep the file name as is. Click OK to proceed.
    name pattern

Application

In this section, we are going to apply the iterator pattern to model the use of iterator in a diagram editor.

  1. Create a new project Diagram 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 design pattern
  4. In the Design Pattern dialog box, select Iterator from the list of patterns.
    select iterator
  5. Click on Aggregate in the overview.
    select aggregate
  6. Rename Aggregate to AbstractList at the bottom pane.
    rename aggregate
  7. Select ConcreteAggregate in overview, and rename it to ShapeList at the bottom pane.
    rename concrete aggregate
  8. Select ConcreteIterator in overview, and rename it to ShapeListIterator at the bottom pane.
    rename concrete iter
  9. Click OK to apply the pattern to diagram.
  10. Tidy up the diagram. Here is the result:
    result



Turn every software project into a successful one.