mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-23 18:25:29 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
+18
-16
@@ -1,23 +1,25 @@
|
||||
---
|
||||
title: Observer
|
||||
title: "Observer Pattern in Java: Mastering Reactive Interfaces in Java Applications"
|
||||
shortTitle: Observer
|
||||
description: "Learn the Observer design pattern in Java. Discover its intent, applicability, and real-world examples. Understand how it promotes loose coupling and dynamic observer management. Ideal for software developers and architects."
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Event-driven
|
||||
- Gang Of Four
|
||||
- Publish/subscribe
|
||||
- Decoupling
|
||||
- Event-driven
|
||||
- Gang Of Four
|
||||
- Publish/subscribe
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Dependents
|
||||
|
||||
## Intent
|
||||
## Intent of Observer Design Pattern
|
||||
|
||||
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
|
||||
The Observer pattern in Java defines a one-to-many relationship between objects, ensuring that when one object updates its state, all dependent observers are notified and updated automatically, enhancing system responsiveness and modularity.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Observer Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -25,13 +27,13 @@ Real-world example
|
||||
|
||||
In plain words
|
||||
|
||||
> Register as an observer to receive state changes in the object.
|
||||
> Implement the Observer interface to actively monitor and respond to state changes in Java applications, improving event-driven programming efficiency.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> The observer pattern is a software 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.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Observer Pattern in Java
|
||||
|
||||
In a land far away live the races of hobbits and orcs. Both of them are mostly outdoors, so they closely follow the weather changes. One could say that they are constantly observing the weather.
|
||||
|
||||
@@ -161,7 +163,7 @@ Program output:
|
||||
21:28:08.313 [main] INFO com.iluwatar.observer.generic.GenHobbits -- The hobbits are facing Sunny weather now
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Observer Pattern in Java
|
||||
|
||||
Use the Observer pattern in any of the following situations:
|
||||
|
||||
@@ -169,7 +171,7 @@ Use the Observer pattern in any of the following situations:
|
||||
* When a change to one object requires changing others, and you don't know how many objects need to be changed.
|
||||
* When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Observer Pattern in Java
|
||||
|
||||
* [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html)
|
||||
* [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html)
|
||||
@@ -178,11 +180,11 @@ Use the Observer pattern in any of the following situations:
|
||||
* Model-View-Controller (MVC) frameworks.
|
||||
* Event handling systems.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Observer Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
* Promotes loose coupling between the subject and its observers.
|
||||
* This Java design pattern promotes loose coupling, allowing the subject and its observers to interact without tight dependencies, facilitating easier maintenance and scalability.
|
||||
* Allows dynamic subscription and unsubscription of observers.
|
||||
|
||||
Trade-offs:
|
||||
@@ -191,12 +193,12 @@ Trade-offs:
|
||||
* The order of notification is not specified, leading to potential unexpected behavior.
|
||||
* Potential for performance issues with a large number of observers.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Mediator](https://java-design-patterns.com/patterns/mediator/): Encapsulates how a set of objects interact, which can be used to reduce the direct dependencies among objects.
|
||||
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used with the Observer pattern to ensure a single instance of the subject.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Java Generics and Collections](https://amzn.to/3VhOBxp)
|
||||
|
||||
Reference in New Issue
Block a user