Skip to content

Commit 317a599

Browse files
committed
Merge pull request iluwatar#337 from mikulucky/master
Provide a simple example for Callback Pattern using Lambdas
2 parents e0e5132 + e25ef1b commit 317a599

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar.callback;
2+
3+
/**
4+
*
5+
* This example generates the exact same output as {@link App} however the callback has been
6+
* defined as a Lambdas expression.
7+
*
8+
*/
9+
public class LambdasApp {
10+
11+
/**
12+
* Program entry point
13+
*/
14+
public static void main(String[] args) {
15+
Task task = new SimpleTask();
16+
Callback c = () -> System.out.println("I'm done now.");
17+
task.executeWith(c);
18+
}
19+
}

callback/src/test/java/com/iluwatar/callback/AppTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,22 @@ public void call() {
3636
assertEquals("Callback called twice", new Integer(2), callingCount);
3737

3838
}
39+
40+
@Test
41+
public void testWithLambdasExample() {
42+
Callback callback = () -> callingCount++;
43+
44+
Task task = new SimpleTask();
45+
46+
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
47+
48+
task.executeWith(callback);
49+
50+
assertEquals("Callback called once", new Integer(1), callingCount);
51+
52+
task.executeWith(callback);
53+
54+
assertEquals("Callback called twice", new Integer(2), callingCount);
55+
56+
}
3957
}

0 commit comments

Comments
 (0)