This post gives a brief overview about the Observer Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML. The post aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Observer Pattern?

According to Wikipedia, the observer pattern is a software design pattern in which an 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. It is mainly used to implement distributed event handling systems, in “event driven” software.

The pattern addresses these problems:

  • A one-to-many dependency between objects should be defined without making the objects tightly coupled.
  • It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically.
  • It should be possible that one object can notify an open-ended number of other objects.

UML Diagram

The following diagram shows the Observer Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:</p><p>@startuml</p><p>title Observer Pattern</p><p>class Subject <<interface>><br /> class Observer <<interface>><br /> class ConcreteSubject<br /> class ConcreteObserver</p><p>Subject : registerObserver(Observer)<br /> Subject : removeObserver(Observer)<br /> Subject : notifyObservers()</p><p>Observer : update()<br /> ConcreteObserver : update()<br /> ConcreteObserver : // otherObserverMethods()</p><p>ConcreteSubject : registerObserver(Observer) {…}<br /> ConcreteSubject : removeObserver(Observer) {…}<br /> ConcreteSubject : notifyObservers() {…}</p><p>Subject “1” -right-> “*” Observer : “observers”</p><p>ConcreteSubject <-left- ConcreteObserver : “subject”<br /> Subject <|– ConcreteSubject<br /> Observer <|— ConcreteObserver</p><p>note left of Subject<br /> The Subject Interface<br /> Objects use this interface to register<br /> as observers and also to remove<br /> themselves from being observers.<br /> end note</p><p>note left of ConcreteSubject<br /> A concrete subject always<br /> implements the Subject<br /> interface. In addition to<br /> the register and remove<br /> methods, the concrete subject<br /> implements a notifyObservers()<br /> method that is used to update<br /> all the current observers<br /> whenever state changes.<br /> end note</p><p>note bottom of ConcreteSubject<br /> The conrete subject<br /> may also have methods for<br /> setting and getting its state.<br /> end note</p><p>note bottom of ConcreteObserver<br /> Concrete observers can be<br /> any class that implements the<br /> Observer interface. Each<br /> observer registers with a concrete<br /> subject to receive updates.<br /> end note</p><p>note top of Observer<br /> All potential observers need<br /> to implement the Observer<br /> interface. This interface<br /> just has one method, update(),<br /> that gets called when the<br /> Subject’s state changes.<br /> end note</p><p>@enduml</p><p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:

@startuml
title Observer Pattern

class Subject <<interface>>
class Observer <<interface>>
class ConcreteSubject
class ConcreteObserver

Subject : registerObserver(Observer)
Subject : removeObserver(Observer)
Subject : notifyObservers()

Observer : update()
ConcreteObserver : update()
ConcreteObserver : // otherObserverMethods()

ConcreteSubject : registerObserver(Observer) {...}
ConcreteSubject : removeObserver(Observer) {...}
ConcreteSubject : notifyObservers() {...}

Subject "1" -right-> "*" Observer : "observers"

ConcreteSubject <-left- ConcreteObserver : "subject"
Subject <|-- ConcreteSubject
Observer <|-- ConcreteObserver
@enduml 

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

Here is a list of posts with other Design Patterns:

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]