diff --git a/trampoline/.gitignore b/trampoline/.gitignore new file mode 100644 index 000000000..431845ed0 --- /dev/null +++ b/trampoline/.gitignore @@ -0,0 +1,2 @@ +/target/ +.idea/ diff --git a/trampoline/README.md b/trampoline/README.md new file mode 100644 index 000000000..849cc663e --- /dev/null +++ b/trampoline/README.md @@ -0,0 +1,38 @@ +--- +layout: pattern +title: Trampoline +folder: trampoline +permalink: /patterns/trampoline/ +categories: Behavior +tags: + - Java + - Difficulty-Intermediate + - Performance + - Recursion +--- + +## Intent +By representing a computation in one of 2 states +(completed with result, or a reference to the reminder of the computation, +something like the way a java.util.Supplier does) +it is possible to implement algorithms recursively in Java without blowing the stack +and to interleave the execution of functions without hard coding them together or even using threads. + + + +## Applicability +Use the Trampoline pattern when + +* For implementing tail recursive function. This pattern allows to switch on a stackless operation. +* For to 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. + +## Credits + +* [Trampolining: a practical guide for awesome Java Developers](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076) +* [Trampoline in java ](http://mindprod.com/jgloss/trampoline.html) + diff --git a/trampoline/pom.xml b/trampoline/pom.xml new file mode 100644 index 000000000..c4cef6860 --- /dev/null +++ b/trampoline/pom.xml @@ -0,0 +1,67 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.19.0-SNAPSHOT + + trampoline + + + + junit + junit + 4.12 + test + + + + org.projectlombok + lombok + 1.16.18 + provided + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19 + + false + + + + + diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/App.java b/trampoline/src/main/java/com/iluwatar/trampoline/App.java new file mode 100644 index 000000000..8afda31a7 --- /dev/null +++ b/trampoline/src/main/java/com/iluwatar/trampoline/App.java @@ -0,0 +1,4 @@ +package com.iluwatar.trampoline; + +public class App { +}