Observer Design Pattern In Java

Observer Design Pattern In Java

Observer Pattern maintains a one-to-many dependency between a Subject and its dependents so that when the state of the Subject changes, dependents get notified.

Today, I will discuss simple and very useful behavioral design pattern called — Observer Design Pattern. This design pattern is useful when we want get notified about changes in the object state.

Observer Design Pattern

  • The Observer Design Pattern maintains one-to-many dependency between Subject (Observable) and its dependents (Observer) in such a way that whenever state of Subject changes, its dependents get notified.
  • The Observer Design Pattern is a 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. 
  • The Observer Design Pattern is used when we like to have notification upon changes in the objects state.
  • The Observer Design Pattern is one of twenty-three well known Gang of Four design patterns that defines an one-to-many dependency objects so that when one object changes state, all of its dependents get notified and updated automatically.
  • The Observer Design Pattern is mainly used to implement distributed event handling , in "event driven" system.
  • In such systems, the subject is usually called a "source of events", while the observers are called "sink of events".
  • Most modern languages have built-in "event" constructs which implement the observer pattern components.
  • Java also offers Observer and Observable to support built-in event constructs for implementing observer pattern. These are available since Java 1. 
  • But, in Java 9 these are declared deprecated/obsolete because the event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrentpackage. For reactive streams style programming, see the Flow API. (read more on Deprecate Observer and Observable). 

observer design pattern

Now an example to understand the implementation of Observer Design Pattern.

Suppose there are some public figures like politicians or celebrities for which there are some followers. Whenever these public figures do any tweet, there registered followers get the notification on that.

java design pattern

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Proxy Design Pattern in JAVA

In this blog, we will discuss Proxy Design Pattern, its example, and how it is different from the other design patterns. But, it’s important to have an understanding of the design patterns first. You can refer here.

Java Design Patterns for Beginners - Design Patterns in Java

"Java Design Patterns for Beginners - Design Patterns in Java - Design Patterns Tutorial" will provide you with detailed knowledge about Java Design Patterns and along with it. Why do we need Design Patterns? Structure of Design Patterns. Types of Design Patterns. Creational Design Patterns. Factory Design Pattern. Overview of Design Patterns

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Composite Design Pattern in Java

This post looks at how to use the composite design pattern in Java, specifically the objects used in the pattern: component, composite, leaf, and client.

Memento Design Pattern In Java

Today, I would like to discuss another behavioral design pattern called the Memento Design Pattern which is used to restore the state of an object to a previous state.