mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-20 07:26:49 +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:
+20
-18
@@ -1,23 +1,25 @@
|
||||
---
|
||||
title: Double Dispatch
|
||||
title: "Double Dispatch Pattern in Java: Enhancing Polymorphic Behavior"
|
||||
shortTitle: Double Dispatch
|
||||
description: "Learn the Double Dispatch Pattern in Java with detailed implementation examples. Understand how to use this design pattern to enhance your Java applications. Read our comprehensive guide."
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Dynamic typing
|
||||
- Polymorphism
|
||||
- Runtime
|
||||
- Decoupling
|
||||
- Dynamic typing
|
||||
- Polymorphism
|
||||
- Runtime
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Multi-methods
|
||||
|
||||
## Intent
|
||||
## Intent of Double Dispatch Design Pattern
|
||||
|
||||
The Double Dispatch pattern is used to achieve dynamic polymorphism based on the types of two objects involved in a method call. It allows method behavior to be different based on the combination of the runtime types of both the object on which the method is called and the object being passed as a parameter.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Double Dispatch Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -25,15 +27,15 @@ Real-world example
|
||||
|
||||
In plain words
|
||||
|
||||
> The Double Dispatch design pattern allows a program to select a different function to execute based on the types of two objects involved in a call, enhancing flexibility in handling interactions between them.
|
||||
> The Double Dispatch design pattern in Java allows a program to select a different function to execute based on the types of two objects involved in a call, enhancing flexibility in handling interactions between them.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In software engineering, double dispatch is a special form of multiple dispatch, and a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call. In most object-oriented systems, the concrete function that is called from a function call in the code depends on the dynamic type of a single object and therefore they are known as single dispatch calls, or simply virtual function calls.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Double Dispatch Pattern in Java
|
||||
|
||||
The Double Dispatch pattern is used to handle collisions between different types of game objects. Each game object is an instance of a class that extends the `GameObject` abstract class. The `GameObject` class has method `collision`, which is overridden in each subclass to define the behavior when a collision occurs with another game object. Here is a simplified version of the `GameObject` class and its subclasses:
|
||||
The Double Dispatch pattern in Java is used to handle collisions between different types of game objects. Each game object is an instance of a class that extends the `GameObject` abstract class. The `GameObject` class has a `collision(GameObject)` method, which is overridden in each subclass to define the behavior when a collision occurs with another game object. Here is a simplified version of the `GameObject` class and its subclasses:
|
||||
|
||||
```java
|
||||
public abstract class GameObject {
|
||||
@@ -61,7 +63,7 @@ public class SpaceStationMir extends GameObject {
|
||||
}
|
||||
```
|
||||
|
||||
In the `App` class's `main` method, the Double Dispatch pattern is used to check for collisions between all pairs of game objects:
|
||||
In the App class, the Double Dispatch pattern is used to check for collisions between all pairs of game objects:
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
@@ -89,7 +91,7 @@ public static void main(String[] args) {
|
||||
}
|
||||
```
|
||||
|
||||
When a collision is detected between two objects, the `collision(GameObject)` method is called on the first object (o1) with the second object (o2) as the argument. This method call is dispatched at runtime to the appropriate `collision(GameObject)` method in the class of o1. Inside this method, another method call `gameObject.collisionWithX(this)` is made on o2 (where X is the type of o1), which is dispatched at runtime to the appropriate `collisionWithX(GameObject)` method in the class of o2. This is the "double dispatch" - two method calls are dispatched at runtime based on the types of two objects.
|
||||
When a collision is detected between two objects, the `collision(GameObject)` method is called on the first object (o1) with the second object (o2) as the argument. This method call is dispatched at runtime to the appropriate `collision(GameObject)` method in the class of o1. Inside this method, another method call `gameObject.collisionWithX(this)` is made on o2 (where X is the type of o1), which is dispatched at runtime to the appropriate `collisionWithX(GameObject)` method in the class of o2. This is the "double dispatch" in Java - two method calls are dispatched at runtime based on the types of two objects.
|
||||
|
||||
Here is the program output:
|
||||
|
||||
@@ -111,22 +113,22 @@ Here is the program output:
|
||||
15:47:23.773 [main] INFO com.iluwatar.doubledispatch.App -- SpaceStationIss at [12,12,14,14] damaged=true onFire=false
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
## Detailed Explanation of Double Dispatch Pattern with Real-World Examples
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
## When to Use the Double Dispatch Pattern in Java
|
||||
|
||||
* When the behavior of a method needs to vary not just based on the object it is called on, but also based on the type of the argument.
|
||||
* In scenarios where if-else or switch-case type checks against the type of objects are cumbersome and not scalable.
|
||||
* When implementing operations in domain classes without contaminating their code with complex decision-making logic about other domain classes.
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Double Dispatch Pattern in Java
|
||||
|
||||
* Graphical user interfaces where different actions are taken based on different types of mouse events interacting with different types of elements.
|
||||
* Simulation systems where interactions between different types of objects need to trigger distinct behaviors.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Double Dispatch Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -138,12 +140,12 @@ Trade-offs:
|
||||
* Can lead to more complex code structures, especially in languages like Java that do not support this pattern natively.
|
||||
* May require additional effort in maintaining and extending as new classes are added.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Similar in intent where it's used to choose an algorithm at runtime, though Strategy focuses on single object context rather than interactions between multiple objects.
|
||||
* [Visitor](https://java-design-patterns.com/patterns/visitor/): Often used together with Double Dispatch to encapsulate operations performed on a set of element objects.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/4awj7cV)
|
||||
* [Java Design Pattern Essentials](https://amzn.to/3Jg8ZZV)
|
||||
|
||||
Reference in New Issue
Block a user