Please support this project by simply putting a Github Star ⭐
Share this library with friends on Twitter and everywhere else you can
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.
- 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.
- 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.
- 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.
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.
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.
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.
No animals were harmed in the making of this library.
The source code comes under the liberal MIT License, making observer-pattern great for all types of applications.
gradle (click to expand)
Replace the variable ${latestVersion} with the current latest version:
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")
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 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)
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()
}
}
}
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:
or over bitcoin(BTC) with this address:
bc1ql2y99q7e8psndhcc3gferk03esw3qqf677rhjy
or over FIO with this address:
FIO7tFMUVAA9cHiPPqKMfMXiSxHrbpiFyRYqTketNuM67aULuwjop
or over Ethereum(ETH) with:
0xc057D159D3C8f3311E73568b334FF6fE82EB2b7D
or over Ethereum Classic(ETC) with:
0xF708cA86D86C246B69c3F4BAe431eBbe0c2bfddD
or over Dogecoin(DOGE) with:
D5yi4Um8cpakd6yPRm2hGWuQ5nrVzhSSW1
or over Monero(XMR) with:
49bqeRQ7Bf49oJFVC72pqpe5hFbb62pfXDYPdLsadGGF81KZW2ZfrPZ8PbAVu5X2v1TYAspeczMya3cYQysNS4usRRPQHVw
or over the donation buttons at the top.
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.
You can donate or contribute solve issues or pull request. Every support are welcome.
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.
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.
- Fork the repository
- Create your feature branch (git checkout -b feature/AmazingFeature)
- Commit your changes (git commit -m 'Add some AmazingFeature')
- Push to the branch (git push origin feature/AmazingFeature)
- Open a Pull
- 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.
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.