Skip to content

astrapi69/observer-pattern

Repository files navigation

Java CI with Gradle Coverage Status Open Issues Maven Central Javadocs MIT License Donate Hits Of Code

Please support this project by simply putting a Github Star ⭐

Share this library with friends on Twitter and everywhere else you can

If you love this project Donation

Observer Design Pattern in Java

Welcome to the Observer Design Pattern project! This repository provides a comprehensive implementation and demonstration of the Observer design pattern in Java. Whether you're a beginner looking to understand design patterns or an experienced developer seeking a robust implementation, this project is for you. Overview

The Observer pattern is a behavioral design pattern where an object, known as the "Subject," maintains a list of its dependents, called "Observers," and notifies them automatically of any state changes. This pattern is widely used in scenarios where a change in one object requires changes in others without tightly coupling the objects.

This project includes:

  • Core interfaces and abstract classes that form the foundation of the Observer pattern.
  • Concrete implementations that demonstrate real-world applications, such as handling exceptions, displaying messages, and more.
  • Swing-based GUI examples to visually showcase the Observer pattern in action.

Features

  • Modular Design: Easily extend the base classes and interfaces to fit your specific use case.
  • Real-World Examples: Understand the Observer pattern through practical examples, including exception handling, event management, and chat systems.
  • Swing GUI: Explore visual demonstrations of the pattern with Java Swing, enhancing your understanding of how the Observer pattern can be applied in desktop applications.
  • Comprehensive Javadoc: Well-documented classes and methods to help you quickly grasp the concepts and integrate them into your projects.

Project Structure

  • src/main/java/io/github/astrapi69/design/pattern/observer/: Contains the core interfaces, abstract classes, and concrete implementations of the Observer pattern.
  • src/test/java/io/github/astrapi69/design/pattern/observer/: Includes test cases to verify the functionality and demonstrate usage.
  • src/test/java/io/github/astrapi69/design/pattern/observer/chat/example/: Contains examples demonstrating the Observer pattern using a chat application with a Swing-based GUI.

Examples

Example 1: Exception Handling with Observer Pattern

The ExceptionEventObserver, ShowExceptionView, and GetStacktraceDisplayView classes demonstrate how to handle exceptions using the Observer pattern. Each observer reacts to changes in the ExceptionEventSubject, either by displaying the exception message or printing the stack trace.

Example 2: Chat Application

The ChatApp demonstrates a simple chat room where users can send and receive messages. The ChatRoomUser class observes the ChatRoom for new messages and updates the GUI accordingly.

Example 3: Event Management

The DemonstrateEventObserver class shows how to manage events, such as state changes, using the Observer pattern. The StateSubject and EventObserver work together to update the state and notify observers.

Note

No animals were harmed in the making of this library.

License

The source code comes under the liberal MIT License, making observer-pattern great for all types of applications.

Import dependencies to your project

gradle (click to expand)

gradle dependency

Replace the variable ${latestVersion} with the current latest version: Maven Central

You can first define the version in the ext section and add than the following gradle dependency to your project build.gradle if you want to import the core functionality of observer-pattern:

define version in file gradle.properties

observerPatternVersion=${latestVersion}

or in build.gradle ext area

    observerPatternVersion = "${latestVersion}"

then add the dependency to the dependencies area

    implementation("io.github.astrapi69:observer-pattern:$observerPatternVersion")

with new libs.versions.toml file

If you use the new libs.versions.toml file for new automatic catalog versions update

[versions]
observer-pattern-version=${latestVersion}

[libraries]
observer-pattern = { module = "io.github.astrapi69:observer-pattern", version.ref = "observer-pattern-version" }

then add the dependency to the dependencies area

    implementation libs.observer.pattern
Maven (click to expand)

Maven dependency

Maven dependency is now on sonatype. Check out sonatype repository for latest snapshots and releases.

Add the following maven dependency to your project pom.xml if you want to import the core functionality of observer-pattern:

Then you can add the dependency to your dependencies:

<properties>
    ...
        <!-- observer-pattern version -->
        <observer-pattern.version>${latestVersion}</observer-pattern.version>
    ...
</properties>
    ...
    <dependencies>
    ...
            <!-- observer-pattern DEPENDENCY -->
            <dependency>
                <groupId>io.github.astrapi69</groupId>
                <artifactId>observer-pattern</artifactId>
                <version>${observer-pattern.version}</version>
            </dependency>
    ...
    </dependencies>
Snapshots (click to expand)

📸 Snapshots

Snapshot

This section describes how to import snapshot versions into your project. Add the following code snippet to your gradle file in the repositories section:

repositories {
   //...
    maven {
        name "Sonatype Nexus Snapshots"
        url "https://oss.sonatype.org/content/repositories/snapshots"
        mavenContent {
            snapshotsOnly()
        }
    }
}

Donations

This project is kept as an open source product and relies on contributions to remain being developed. If you like this library, please consider a donation

over paypal:

PayPal this

or over bitcoin(BTC) with this address:

bc1ql2y99q7e8psndhcc3gferk03esw3qqf677rhjy

Donation Bitcoin Wallet

or over FIO with this address:

FIO7tFMUVAA9cHiPPqKMfMXiSxHrbpiFyRYqTketNuM67aULuwjop

Donation FIO Wallet

or over Ethereum(ETH) with:

0xc057D159D3C8f3311E73568b334FF6fE82EB2b7D

Donation Ethereum Wallet

or over Ethereum Classic(ETC) with:

0xF708cA86D86C246B69c3F4BAe431eBbe0c2bfddD

Donation Ethereum Classic Wallet

or over Dogecoin(DOGE) with:

D5yi4Um8cpakd6yPRm2hGWuQ5nrVzhSSW1

Donation Dogecoin Wallet

or over Monero(XMR) with:

49bqeRQ7Bf49oJFVC72pqpe5hFbb62pfXDYPdLsadGGF81KZW2ZfrPZ8PbAVu5X2v1TYAspeczMya3cYQysNS4usRRPQHVw

Donation Monero Wallet

or over the donation buttons at the top.

Semantic Versioning

The versions of observer-pattern are maintained with the Semantic Versioning guidelines.

Release version numbers will be incremented in the following format:

<major>.<minor>.<patch>

For detailed information on versioning you can visit the wiki page.

What can i do to support this project

You can donate or contribute solve issues or pull request. Every support are welcome.

Want to Help and improve it?

The source code for observer-pattern are on GitHub. Please feel free to fork and send pull requests!

Create your own fork of astrapi69/observer-pattern/fork

To share your changes, submit a pull request.

Don't forget to add new units tests on your changes.

Contributing

Contributions are welcome! If you have ideas for new features, find a bug, or want to improve the documentation, feel free to submit an issue or pull request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull

Acknowledgements

  • Inspired by the classic design patterns described in the "Gang of Four" book.
  • Special thanks to the open-source community for their contributions and feedback.

Contacting the Developers

Do not hesitate to contact the observer-pattern developers with your questions, concerns, comments, bug reports, or feature requests.

  • Feature requests, questions and bug reports can be reported at the issues page.

Similar projects

Credits