X Tutup
Skip to content

Commit eee2160

Browse files
committed
Merge pull request iluwatar#36 from vehpsr/master
added Callback pattern
2 parents 33362d6 + bf2df42 commit eee2160

File tree

10 files changed

+140
-2
lines changed

10 files changed

+140
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi
5050
* [Visitor](#visitor)
5151
* [Double Checked Locking](#double-checked-locking)
5252
* [Null Object](#null-object)
53+
* [Callback](#callback)
5354

5455
## <a name="abstract-factory">Abstract Factory</a> [&#8593;](#list-of-design-patterns)
5556
**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
412413
**Applicability:** Use the Event Aggregator pattern when
413414
* 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.
414415

416+
## <a name="callback">Callback</a> [&#8593;](#list-of-design-patterns)
417+
**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.
418+
419+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/callback/etc/callback.jpg "Callback")
420+
421+
**Applicability:** Use the Callback pattern when
422+
* When some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
423+
415424

416425
# Frequently asked questions
417426

callback/etc/callback.jpg

15.1 KB
Loading

callback/etc/callback.ucls

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="JPEG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.SimpleTask" project="callback"
5+
file="/callback/src/main/java/com/iluwatar/SimpleTask.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="358" y="315"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<interface id="2" language="java" name="com.iluwatar.Callback" project="callback"
14+
file="/callback/src/main/java/com/iluwatar/Callback.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="529" y="165"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</interface>
22+
<class id="3" language="java" name="com.iluwatar.Task" project="callback"
23+
file="/callback/src/main/java/com/iluwatar/Task.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="356" y="163"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<generalization id="4">
32+
<end type="SOURCE" refId="1"/>
33+
<end type="TARGET" refId="3"/>
34+
</generalization>
35+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
36+
sort-features="false" accessors="true" visibility="true">
37+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
38+
<operations public="true" package="true" protected="true" private="true" static="true"/>
39+
</classifier-display>
40+
<association-display labels="true" multiplicity="true"/>
41+
</class-diagram>

callback/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>callback</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
/**
4+
* Callback pattern is more native for dynamic languages where function are first-class citizen.
5+
* Prior to Java8 can be simulated using simple (alike command) interfaces.
6+
*/
7+
public class App {
8+
9+
public static void main(String[] args) {
10+
Task task = new SimpleTask();
11+
Callback callback = new Callback() {
12+
@Override
13+
public void call() {
14+
System.out.println("I'm done now.");
15+
}
16+
};
17+
task.executeWith(callback);
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
/**
4+
* Callback interface
5+
*/
6+
public interface Callback {
7+
8+
public void call();
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar;
2+
3+
/**
4+
* Implementation of task that need to be executed
5+
*/
6+
public class SimpleTask extends Task {
7+
8+
@Override
9+
public void execute() {
10+
System.out.println("Perform some important activity.");
11+
}
12+
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar;
2+
3+
/**
4+
* Template-method class for callback hook execution
5+
*/
6+
public abstract class Task {
7+
8+
public final void executeWith(Callback callback) {
9+
execute();
10+
if (callback != null) {
11+
callback.call();
12+
}
13+
}
14+
15+
public abstract void execute();
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
import org.junit.Test;
4+
5+
public class AppTest {
6+
7+
@Test
8+
public void test() {
9+
String[] args = {};
10+
App.main(args);
11+
}
12+
}

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
<module>double-checked-locking</module>
3939
<module>servant</module>
4040
<module>service-locator</module>
41-
<module>null-object</module>
42-
<module>event-aggregator</module>
41+
<module>null-object</module>
42+
<module>event-aggregator</module>
43+
<module>callback</module>
4344
</modules>
4445

4546
<dependencyManagement>

0 commit comments

Comments
 (0)
X Tutup