Skip to content

Commit

Permalink
Created a Lambdas example for the callback pattern, using the same ou…
Browse files Browse the repository at this point in the history
…tput of the traditional version. Have also added another unit test for issue iluwatar#334
  • Loading branch information
mikulucky committed Jan 1, 2016
1 parent e0e5132 commit ca4a4ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions callback/src/main/java/com/iluwatar/callback/LambdasApp.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions callback/src/test/java/com/iluwatar/callback/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ public void call() {
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);

}
}

0 comments on commit ca4a4ec

Please sign in to comment.