mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-17 00:25:54 +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,12 +1,14 @@
|
||||
---
|
||||
title: Trampoline
|
||||
title: "Trampoline Pattern in Java: Mastering Recursion Without Stack Overflow"
|
||||
shortTitle: Trampoline
|
||||
description: "Discover how to implement the Trampoline pattern in Java to efficiently manage recursive functions and prevent stack overflow errors, with real-world examples and programming insights."
|
||||
category: Functional
|
||||
language: en
|
||||
tag:
|
||||
- Code simplification
|
||||
- Functional decomposition
|
||||
- Performance
|
||||
- Recursion
|
||||
- Code simplification
|
||||
- Functional decomposition
|
||||
- Performance
|
||||
- Recursion
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -14,11 +16,11 @@ tag:
|
||||
* Bounce
|
||||
* Tail-Call Optimization
|
||||
|
||||
## Intent
|
||||
## Intent of Trampoline Design Pattern
|
||||
|
||||
To optimize recursive function calls by converting them into iterative loops, avoiding stack overflow errors.
|
||||
The Trampoline Pattern in Java optimizes recursive function calls by converting them into iterative loops, avoiding stack overflow errors.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Trampoline Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -28,13 +30,13 @@ Real-world example
|
||||
|
||||
In plain words
|
||||
|
||||
> Trampoline pattern allows recursion without running out of stack memory.
|
||||
> The Trampoline pattern in Java allows efficient recursion without running out of stack memory, optimizing deep recursive calls for better performance and stack safety.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In Java, trampoline refers to using reflection to avoid using inner classes, for example in event listeners. The time overhead of a reflection call is traded for the space overhead of an inner class. Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Trampoline Pattern in Java
|
||||
|
||||
Here's the `Trampoline` implementation in Java.
|
||||
|
||||
@@ -120,14 +122,14 @@ Program output:
|
||||
19:22:24.472 [main] INFO com.iluwatar.trampoline.TrampolineApp - The number of orcs perished in the war: 3628800
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Trampoline Pattern in Java
|
||||
|
||||
Use the Trampoline pattern when
|
||||
|
||||
* When dealing with algorithms that use recursion heavily and risk running into stack overflow errors.
|
||||
* When tail-call optimization is not supported by the Java language natively.
|
||||
|
||||
## Tutorials
|
||||
## Trampoline Pattern Java Tutorials
|
||||
|
||||
* [Laziness, trampolines, monoids and other functional amenities: This is not your father's Java(Mario Fusco)](https://www.slideshare.net/mariofusco/lazine)
|
||||
* [Trampoline.java (totallylazy)](https://github.com/bodar/totallylazy/blob/master/src/com/googlecode/totallylazy/Trampoline.java)
|
||||
@@ -135,13 +137,13 @@ Use the Trampoline pattern when
|
||||
* [Trampolining: A practical guide for awesome Java Developers (John McClean)](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076)
|
||||
* [What is a trampoline function? (Stack Overflow)](https://stackoverflow.com/questions/189725/what-is-a-trampoline-function)
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Trampoline Pattern in Java
|
||||
|
||||
* Implementing algorithms that require deep recursion, such as certain tree traversals, combinatorial algorithms, and mathematical computations.
|
||||
* Functional programming libraries and frameworks where tail-call optimization is necessary for performance and stack safety.
|
||||
* [cyclops-react](https://github.com/aol/cyclops-react)
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Trampoline Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -154,13 +156,13 @@ Trade-offs:
|
||||
* May introduce additional complexity in terms of understanding and implementing the trampoline mechanism.
|
||||
* Requires converting naturally recursive algorithms into a continuation-passing style.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Iterator](https://java-design-patterns.com/patterns/iterator/): Both patterns aim to transform potentially recursive operations into iterative processes, though the iterator pattern is more general-purpose.
|
||||
* [State](https://java-design-patterns.com/patterns/state/): Like the Trampoline, the State pattern can also handle complex state transitions, which can sometimes involve recursive-like state changes.
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): This pattern can be related in terms of defining a family of algorithms (or continuations in the case of the Trampoline) and making them interchangeable.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)
|
||||
* [Functional Programming for Java Developers: Tools for Better Concurrency, Abstraction, and Agility](https://amzn.to/4dRu4rJ)
|
||||
|
||||
Reference in New Issue
Block a user