Skip to content

Java demo showcasing both the Observer and Publish/Subscribe design patterns. Includes simple, practical implementations to highlight their differences.

Notifications You must be signed in to change notification settings

hamzadabaghi/observer-and-pub-sub-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

The project is a Java application that demonstrates the implementation of the Observer and Publish-Subscribe (Pub/Sub) design patterns. Below is a description of the key components and their roles :

Observer Pattern

observer.architecture Package

  • Observer Interface: Defines the update method that observers must implement.
  • ConcreteObserver Class: Implements the Observer interface and provides a concrete implementation of the update method.
  • Subject Abstract Class: Manages a list of observers using weak references to avoid memory leaks. It provides methods to attach, detach, and notify observers.
  • ConcreteSubject Class: Extends the Subject class and includes state management. When the state changes, it notifies all attached observers.

observer.example Package

  • Stocks Class: Represents a stock data source that observers can attach to. It extends the Subject class.
  • StatuBar Class: An observer that updates a status bar based on stock data.
  • StatusListView Class: An observer that updates a list view based on stock data.

Publish-Subscribe Pattern

pub_sub.architecture Package

  • MessageBroker Class: Manages subscriptions and publishes messages to subscribers based on topics.
  • Message Class: Represents a message that can be published to subscribers.
  • Publisher Class: Represents a publisher that sends messages to the message broker.
  • Subscriber Class: Represents a subscriber that listens for messages on a specific topic.

pub_sub.example Package

  • UserService Class: Publishes user registration events to the message broker.
  • AnalyticsService Class: Subscribes to user registration events and processes them.
  • EmailService Class: Subscribes to user registration events and sends emails.
  • UserRegistrationMessage Class: Represents a message containing user registration details.

Main Class

  • Main Class: Demonstrates the usage of both the Observer and Pub/Sub patterns. It creates instances of subjects and observers, attaches observers to subjects, and triggers state changes to show how observers are notified. It also sets up the message broker, subscribes services to a topic, and publishes events.

This project provides a clear example of how to implement and use the Observer and Pub/Sub design patterns in Java.

About

Java demo showcasing both the Observer and Publish/Subscribe design patterns. Includes simple, practical implementations to highlight their differences.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages