Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: Observer Pattern

Design Patterns with PlantUML: Observer Pattern

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>@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>

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:

Adapter Pattern Design Patterns with PlantUML: Adapter Pattern - This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design ... Read More
Command Pattern Design Patterns with PlantUML: Command Pattern - This post gives a brief overview about the Command Pattern. The post is part of a series about software design ... Read More
Singleton Design Patterns with PlantUML: Singleton - This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Abstract Factory Pattern - This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Factory Method Pattern - This post gives a brief overview about the Factory Method Pattern. The post is part of a series about software ... Read More
Decorator Pattern Design Patterns with PlantUML: Decorator Pattern - This post gives a brief overview about the Decorator Pattern. The post is part of a series about software design ... Read More
Observer Pattern Design Patterns with PlantUML: Observer Pattern - This post gives a brief overview about the Observer Pattern. The post is part of a series about software design ... Read More

Check Also

Adapter Pattern

Design Patterns with PlantUML: Adapter Pattern

This post gives a brief overview about the Adapter Pattern. The post is part of …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use Cookies and similar technology to collect and analyse information about the users of this website. We use this information to enhance the content, advertising and other services available on the site. Please click ‘Accept cookies’ to consent to the use of this technology by petrockblock. You can manage your preferences at any time by visiting our Cookies Policy page.