Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Member-only story

Easy Python Patterns: The Observer pattern — Code nomad

--

Press enter or click to view image in full size
Picture by Lucas Pezeta: https://www.pexels.com/nl-nl/foto/black-telescope-under-blue-en-blacksky-2034892/

Introduction

The observer pattern is a software design pattern that allows an object, usually called the subject, to maintain a list of dependents, called observers, and notify them automatically of any state-changes. Many languages have this pattern either built-in or in their standard libraries.

Examples:

  • C# has the concept of Observable in the standard .NET library. Also it has the concept of delegates.
  • Although Dart doesn’t have it, in Flutter we have the SetState method which automatically notifes any observer, and it is mainly used to refresh the UI.

It looks like this:

Press enter or click to view image in full size

A breakdown of this diagram:

  1. First there is the Observable, this could be an interface, the object which is to be observed. The observable holds a list of observers.
  2. The Observer, which sends out a message to the observers. This could also be an interface.
  3. The ConcreteObservable, the concrete class which holds the state. If anything changes in the state, the setState method is called, which in turn…

--

--

The Code Nomad
The Code Nomad

Written by The Code Nomad

Fueled by a passion for code and music, with 25+ years in IT and a lifelong soundtrack playing in the background—that's what keeps me going.

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.