Jump to Menu

Observer Pattern Tutorial

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

Compatible edition(s): Enterprise, Professional, Standard

  • October 21, 2009
  • Views: 35,291
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Observer.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Subject.
    create subject
  4. Right-click on Subject, and select Model Element Properties > Abstract to set it as abstract.
    set subject abstract
  5. Right-click on Subject class, and select Add > Operation from the popup menu.
    add oper to subject
  6. Name the operation Attach(o : Observer).
  7. Repeat step 5 and 6 to create the remaining two operations : Detach (o : Observer), Notify().
    added oper to subject
  8. Move the mouse cursor over the Subject class, and drag out Generalization > Class to create a subclass ConcreteSubject.
    create concrete subject
  9. Repeat steps 5 and 6 to create the following operations in ConcreteSubject: GetState(), SetState(state).
    added oper to concrete subject
  10. Right-click on the ConcreteSubject class, and select Add > Attribute from the popup menu. Name the attribute subjectstate.
    added attr to concrete subject
  11. Move the mouse cursor over the Subject class, and drag out Association > Class to create an associated class Observer.
    created observer
  12. Right-click an Observer, and select Model Element Properties > Abstract to set it as abstract.
  13. Repeat steps 5 and 6 to create the following operation in Observer: Update().
    added update to observer
  14. Right-click on Update(), and select Model Element Properties > Abstract to set it as abstract.
    set update oper abstract
  15. Move the mouse cursor over the Observer class, and drag out Generalization > Class to create a subclass ConcreteObserver.
    created concrete observer
  16. ConcreteObserver will inherits the operations from Observer. Right-click on ConcreteObserver and select Related Elements > Realize all Interfaces from the popup menu.
    realize concrete observer
  17. Right-click on the ConcreteObserver class, and select Add > Attribute from the popup menu. Name the attribute observerstate. Up to now, the diagram should look like:
    pattern modeled without stereotype
  18. In practice, there may be multiple concrete subjects and observers. To represent this, stereotype the class ConcreteSubject and ConcreteObserver as PTN Cloneable. Right-click on ConcreteSubject and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete subject
  19. In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to ConcreteSubject class. Click OK to confirm.
    set class ptn cloneable
  20. Repeat steps 18 and 19 on ConcreteObserver.
    concrete classes stereotyped
  21. In practice, there are domain specific operations in subject and observer. To represent this, stereotype the class Subject and Observer as PTN Members Creatable. Repeat steps 18 and 19 to stereotype Subject and Observer as PTN Members Creatable.
    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 design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Observer. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

In this section, we are going to apply the observer pattern to model a diagram editor for observing changes of model, and calling various panes like the property and overview panes to update their content.

  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 Observer from the list of patterns.
    select observer
  5. At the bottom pane, rename classes Subject, Observer, ConcreteSubject and ConcreteObserver to Shape, Pane, Rectangle and PropertyPane respectively.
    rename classes
  6. Besides rectangle, there are more types of shape like circle and triangle. Select ConcreteSubject from the overview pane.
    select concrete subject
  7. Click on the + button next to the class name and select Clone... from the popup menu.
    clone
  8. Enter 2, which is the number of classes to clone, and click OK to confirm.
    enter clone count
  9. At the bottom pane, rename ConcreteSubject2 and ConcreteSubject3 to Circle and Triangle.
    rename concrete subject
  10. For observers, there are also panes like overview pane. Select ConcreteObserver from the overview pane.
    select concrete observer
  11. Click on the + button next to the class name and select Clone... from the popup menu.
  12. Enter 1, which is the number of classes to clone, and click OK to confirm.
  13. At the bottom pane, rename ConcreteObserver2 to OverviewPane.
    rename concrete observer
  14. Click OK to confirm. Here is the diagram formed:
    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