In this tutorial, we will explore the powerful Gang of Four (GoF) observer design pattern. By the end of this guide, you will have a solid understanding of how to define and apply the observer pattern in your software projects. We will walk you through creating a UML class diagram for the observer pattern and show you how to save it as a design pattern file that can be easily reused in the future. Whether you are new to design patterns or an experienced software developer, this tutorial will provide you with the tools you need to implement the observer pattern in your projects.

What is Observer Design Pattern?

The Observer Design Pattern is a software design pattern that defines a one-to-many dependency relationship between objects. In this pattern, one object (called the “subject”) maintains a list of its dependents (called “observers”) and notifies them automatically of any state changes, usually by calling one of their methods.

The purpose of the Observer pattern is to decouple the subject from its observers, allowing them to vary independently of each other. This means that changes to the subject will not affect the observers directly, and new observers can be added or removed from the list without modifying the subject.

The Observer pattern is commonly used in event-driven systems, where the subject is an event source and the observers are event listeners. It is also used in user interface frameworks, where changes to the user interface are reflected in real-time across all observers. The Observer pattern is one of the most widely used design patterns in software development and is an important concept for any programmer to understand.

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

Saving the 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 SubjectObserverConcreteSubject and ConcreteObserver to ShapePaneRectangle 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

Resources

  1. Design Patterns.vpp
  2. Observer.pat

Related Links

Leave a Comment

Your email address will not be published.