diff --git a/README.md b/README.md index 464de466d..24735a590 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi * [Visitor](#visitor) * [Double Checked Locking](#double-checked-locking) * [Null Object](#null-object) +* [Callback](#callback) ## Abstract Factory [↑](#list-of-design-patterns) **Intent:** Provide an interface for creating families of related or dependent objects without specifying their concrete classes. @@ -412,6 +413,14 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi **Applicability:** Use the Event Aggregator pattern when * Event Aggregator is a good choice when you have lots of objects that are potential event sources. Rather than have the observer deal with registering with them all, you can centralize the registration logic to the Event Aggregator. As well as simplifying registration, a Event Aggregator also simplifies the memory management issues in using observers. +## Callback [↑](#list-of-design-patterns) +**Intent:** Callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. + +![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/callback/etc/callback.jpg "Callback") + +**Applicability:** Use the Callback pattern when +* When some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity. + # Frequently asked questions diff --git a/callback/etc/callback.jpg b/callback/etc/callback.jpg new file mode 100644 index 000000000..2462970d9 Binary files /dev/null and b/callback/etc/callback.jpg differ diff --git a/callback/etc/callback.ucls b/callback/etc/callback.ucls new file mode 100644 index 000000000..6fd1323a9 --- /dev/null +++ b/callback/etc/callback.ucls @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/callback/pom.xml b/callback/pom.xml new file mode 100644 index 000000000..96a7aabd7 --- /dev/null +++ b/callback/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + callback + + + junit + junit + test + + + diff --git a/callback/src/main/java/com/iluwatar/App.java b/callback/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..6a36d66d7 --- /dev/null +++ b/callback/src/main/java/com/iluwatar/App.java @@ -0,0 +1,19 @@ +package com.iluwatar; + +/** + * Callback pattern is more native for dynamic languages where function are first-class citizen. + * Prior to Java8 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); + } +} diff --git a/callback/src/main/java/com/iluwatar/Callback.java b/callback/src/main/java/com/iluwatar/Callback.java new file mode 100644 index 000000000..f1c0baf75 --- /dev/null +++ b/callback/src/main/java/com/iluwatar/Callback.java @@ -0,0 +1,9 @@ +package com.iluwatar; + +/** + * Callback interface + */ +public interface Callback { + + public void call(); +} diff --git a/callback/src/main/java/com/iluwatar/SimpleTask.java b/callback/src/main/java/com/iluwatar/SimpleTask.java new file mode 100644 index 000000000..703474a90 --- /dev/null +++ b/callback/src/main/java/com/iluwatar/SimpleTask.java @@ -0,0 +1,13 @@ +package com.iluwatar; + +/** + * Implementation of task that need to be executed + */ +public class SimpleTask extends Task { + + @Override + public void execute() { + System.out.println("Perform some important activity."); + } + +} diff --git a/callback/src/main/java/com/iluwatar/Task.java b/callback/src/main/java/com/iluwatar/Task.java new file mode 100644 index 000000000..ad3a0f14b --- /dev/null +++ b/callback/src/main/java/com/iluwatar/Task.java @@ -0,0 +1,16 @@ +package com.iluwatar; + +/** + * Template-method class for callback hook execution + */ +public abstract class Task { + + public final void executeWith(Callback callback) { + execute(); + if (callback != null) { + callback.call(); + } + } + + public abstract void execute(); +} diff --git a/callback/src/test/java/com/iluwatar/AppTest.java b/callback/src/test/java/com/iluwatar/AppTest.java new file mode 100644 index 000000000..6db5ad214 --- /dev/null +++ b/callback/src/test/java/com/iluwatar/AppTest.java @@ -0,0 +1,12 @@ +package com.iluwatar; + +import org.junit.Test; + +public class AppTest { + + @Test + public void test() { + String[] args = {}; + App.main(args); + } +} diff --git a/pom.xml b/pom.xml index 7316f65f2..7cfff29c4 100644 --- a/pom.xml +++ b/pom.xml @@ -38,8 +38,9 @@ double-checked-locking servant service-locator - null-object - event-aggregator + null-object + event-aggregator + callback