Reformat business-delegate, callback, chain, command, composite, dao, decorator & dependency-injection patterns.
This commit is contained in:
@@ -2,20 +2,21 @@ package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* Callback pattern is more native for functional languages where functions are treated as first-class citizens.
|
||||
* Prior to Java 8 callbacks can be simulated using simple (alike command) interfaces.
|
||||
* Callback pattern is more native for functional languages where functions are treated as
|
||||
* first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command)
|
||||
* interfaces.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Task task = new SimpleTask();
|
||||
Callback callback = new Callback() {
|
||||
@Override
|
||||
public void call() {
|
||||
System.out.println("I'm done now.");
|
||||
}
|
||||
};
|
||||
task.executeWith(callback);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Task task = new SimpleTask();
|
||||
Callback callback = new Callback() {
|
||||
@Override
|
||||
public void call() {
|
||||
System.out.println("I'm done now.");
|
||||
}
|
||||
};
|
||||
task.executeWith(callback);
|
||||
}
|
||||
}
|
||||
|
@@ -7,5 +7,5 @@ package com.iluwatar.callback;
|
||||
*/
|
||||
public interface Callback {
|
||||
|
||||
public void call();
|
||||
public void call();
|
||||
}
|
||||
|
@@ -7,9 +7,8 @@ package com.iluwatar.callback;
|
||||
*/
|
||||
public class SimpleTask extends Task {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("Perform some important activity and after call the callback method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("Perform some important activity and after call the callback method.");
|
||||
}
|
||||
}
|
||||
|
@@ -7,12 +7,12 @@ package com.iluwatar.callback;
|
||||
*/
|
||||
public abstract class Task {
|
||||
|
||||
public final void executeWith(Callback callback) {
|
||||
execute();
|
||||
if (callback != null) {
|
||||
callback.call();
|
||||
}
|
||||
}
|
||||
public final void executeWith(Callback callback) {
|
||||
execute();
|
||||
if (callback != null) {
|
||||
callback.call();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void execute();
|
||||
public abstract void execute();
|
||||
}
|
||||
|
@@ -5,35 +5,35 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Add a field as a counter. Every time the callback method is called increment this
|
||||
* field. Unit test checks that the field is being incremented.
|
||||
* Add a field as a counter. Every time the callback method is called increment this field. Unit
|
||||
* test checks that the field is being incremented.
|
||||
*
|
||||
* Could be done with mock objects as well where the call method call is verified.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
private Integer callingCount = 0;
|
||||
private Integer callingCount = 0;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Callback callback = new Callback() {
|
||||
@Override
|
||||
public void call() {
|
||||
callingCount++;
|
||||
}
|
||||
};
|
||||
@Test
|
||||
public void test() {
|
||||
Callback callback = new Callback() {
|
||||
@Override
|
||||
public void call() {
|
||||
callingCount++;
|
||||
}
|
||||
};
|
||||
|
||||
Task task = new SimpleTask();
|
||||
Task task = new SimpleTask();
|
||||
|
||||
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
||||
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
||||
|
||||
task.executeWith(callback);
|
||||
task.executeWith(callback);
|
||||
|
||||
assertEquals("Callback called once", new Integer(1), callingCount);
|
||||
assertEquals("Callback called once", new Integer(1), callingCount);
|
||||
|
||||
task.executeWith(callback);
|
||||
task.executeWith(callback);
|
||||
|
||||
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user