mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +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,25 +1,27 @@
|
||||
---
|
||||
title: Callback
|
||||
title: "Callback Pattern in Java: Mastering Asynchronous Communication"
|
||||
shortTitle: Callback
|
||||
description: "Learn about the Java Callback Design Pattern, including its intent, usage scenarios, benefits, trade-offs, and real-world examples. Understand how to implement and effectively use callbacks in your Java applications."
|
||||
category: Functional
|
||||
language: en
|
||||
tag:
|
||||
- Asynchronous
|
||||
- Decoupling
|
||||
- Idiom
|
||||
- Reactive
|
||||
- Asynchronous
|
||||
- Decoupling
|
||||
- Idiom
|
||||
- Reactive
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
Callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time.
|
||||
|
||||
## Also known as
|
||||
|
||||
* Call-After
|
||||
* Event-Subscription
|
||||
* Listener
|
||||
|
||||
## Explanation
|
||||
## Intent of Callback Design Pattern
|
||||
|
||||
The Java Callback Design Pattern is a piece of executable code passed as an argument to other code, which is expected to call back (execute) the argument at a convenient time.
|
||||
|
||||
## Detailed Explanation of Callback Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -35,7 +37,7 @@ Wikipedia says
|
||||
|
||||
> In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Callback Pattern in Java
|
||||
|
||||
We need to be notified after the executing task has finished. We pass a callback method for the executor and wait for it to call back on us.
|
||||
|
||||
@@ -87,7 +89,7 @@ Program output:
|
||||
17:12:11.682 [main] INFO com.iluwatar.callback.App -- I'm done now.
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Callback Pattern in Java
|
||||
|
||||
Use the Callback pattern when
|
||||
|
||||
@@ -95,14 +97,14 @@ Use the Callback pattern when
|
||||
* Implementing notification mechanisms where certain events need to trigger actions in other components.
|
||||
* Decoupling modules or components that need to interact without having a direct dependency on each other
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Callback Pattern in Java
|
||||
|
||||
* GUI frameworks often use callbacks for event handling, such as user interactions (clicks, key presses)
|
||||
* Node.js heavily relies on callbacks for non-blocking I/O operations
|
||||
* Frameworks that deal with asynchronous operations, like Promises in JavaScript, use callbacks to handle the resolution or rejection of asynchronous tasks
|
||||
* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept a callback that will be triggered every time a barrier is tripped.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Callback Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -116,13 +118,13 @@ Trade-offs:
|
||||
* Inversion of control can lead to harder-to-follow code flow, making debugging more challenging
|
||||
* Potential issues with error handling, especially in languages or environments where exceptions are used, as errors might need to be propagated through callbacks
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Command](https://java-design-patterns.com/patterns/command/): Callbacks can be implemented as Command objects in scenarios where more flexibility or statefulness is required in the callback operation
|
||||
* [Observer](https://java-design-patterns.com/patterns/observer/): Callbacks can be seen as a more dynamic and lightweight form of the Observer pattern, with the ability to subscribe and unsubscribe callback functions dynamically
|
||||
* [Promise](https://java-design-patterns.com/patterns/promise/): In some languages or frameworks, Promises or Futures can be used to handle asynchronous operations more cleanly, often using callbacks for success or failure cases
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
|
||||
Reference in New Issue
Block a user