Narendra Pathai 2aa9e78ddd
Minor refactorings and code style changes (#807)
* Made minor changes in some patterns such as removed throws clause where not needed, changed incorrect order of arguments in assertEquals

* Minor refactorings and code style changes. 1) Removed several use of raw types 2) Removed unnecessary throws clauses 3) Used lambda expressions wherever applicable 4) Used apt assertion methods for readability 5) Use of try with resources wherever applicable 6) Corrected incorrect order of assertXXX arguments

* Removed unused import from Promise

* Addressed review comments

* Addressed checkstyle issue
2018-10-23 13:45:41 +05:30
..
2018-01-20 13:36:43 +03:00

layout, title, folder, permalink, categories, tags
layout title folder permalink categories tags
pattern Trampoline trampoline /patterns/trampoline/ Behavior
Java
Difficulty-Intermediate
Performance
Recursion

Intent

Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack and to interleave the execution of functions without hard coding them together It is possible by representing a computation in one of 2 states : done | more (completed with result, or a reference to the reminder of the computation, something like the way a java.util.Supplier does).

Explanation

Trampoline pattern allows to define recursive algorithms by iterative loop.

Applicability

Use the Trampoline pattern when

  • For implementing tail recursive function. This pattern allows to switch on a stackless operation.
  • For interleaving the execution of two or more functions on the same thread.

Known uses(real world examples)

  • 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.

Tutorials

Credits