Merge pull request #337 from mikulucky/master
Provide a simple example for Callback Pattern using Lambdas
This commit is contained in:
commit
317a599c3b
19
callback/src/main/java/com/iluwatar/callback/LambdasApp.java
Normal file
19
callback/src/main/java/com/iluwatar/callback/LambdasApp.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package com.iluwatar.callback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This example generates the exact same output as {@link App} however the callback has been
|
||||||
|
* defined as a Lambdas expression.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LambdasApp {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program entry point
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Task task = new SimpleTask();
|
||||||
|
Callback c = () -> System.out.println("I'm done now.");
|
||||||
|
task.executeWith(c);
|
||||||
|
}
|
||||||
|
}
|
@ -36,4 +36,22 @@ public class AppTest {
|
|||||||
assertEquals("Callback called twice", new Integer(2), callingCount);
|
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithLambdasExample() {
|
||||||
|
Callback callback = () -> callingCount++;
|
||||||
|
|
||||||
|
Task task = new SimpleTask();
|
||||||
|
|
||||||
|
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
||||||
|
|
||||||
|
task.executeWith(callback);
|
||||||
|
|
||||||
|
assertEquals("Callback called once", new Integer(1), callingCount);
|
||||||
|
|
||||||
|
task.executeWith(callback);
|
||||||
|
|
||||||
|
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user