added Callback pattern
This commit is contained in:
parent
33362d60ad
commit
bf2df42c55
@ -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)
|
||||
|
||||
## <a name="abstract-factory">Abstract Factory</a> [↑](#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.
|
||||
|
||||
## <a name="callback">Callback</a> [↑](#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.
|
||||
|
||||

|
||||
|
||||
**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
|
||||
|
||||
|
BIN
callback/etc/callback.jpg
Normal file
BIN
callback/etc/callback.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
41
callback/etc/callback.ucls
Normal file
41
callback/etc/callback.ucls
Normal file
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="JPEG" always-add-relationships="false"
|
||||
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.SimpleTask" project="callback"
|
||||
file="/callback/src/main/java/com/iluwatar/SimpleTask.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="358" y="315"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="2" language="java" name="com.iluwatar.Callback" project="callback"
|
||||
file="/callback/src/main/java/com/iluwatar/Callback.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="529" y="165"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="3" language="java" name="com.iluwatar.Task" project="callback"
|
||||
file="/callback/src/main/java/com/iluwatar/Task.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="356" y="163"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<generalization id="4">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
18
callback/pom.xml
Normal file
18
callback/pom.xml
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<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"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>callback</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
19
callback/src/main/java/com/iluwatar/App.java
Normal file
19
callback/src/main/java/com/iluwatar/App.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
9
callback/src/main/java/com/iluwatar/Callback.java
Normal file
9
callback/src/main/java/com/iluwatar/Callback.java
Normal file
@ -0,0 +1,9 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* Callback interface
|
||||
*/
|
||||
public interface Callback {
|
||||
|
||||
public void call();
|
||||
}
|
13
callback/src/main/java/com/iluwatar/SimpleTask.java
Normal file
13
callback/src/main/java/com/iluwatar/SimpleTask.java
Normal file
@ -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.");
|
||||
}
|
||||
|
||||
}
|
16
callback/src/main/java/com/iluwatar/Task.java
Normal file
16
callback/src/main/java/com/iluwatar/Task.java
Normal file
@ -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();
|
||||
}
|
12
callback/src/test/java/com/iluwatar/AppTest.java
Normal file
12
callback/src/test/java/com/iluwatar/AppTest.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
5
pom.xml
5
pom.xml
@ -38,8 +38,9 @@
|
||||
<module>double-checked-locking</module>
|
||||
<module>servant</module>
|
||||
<module>service-locator</module>
|
||||
<module>null-object</module>
|
||||
<module>event-aggregator</module>
|
||||
<module>null-object</module>
|
||||
<module>event-aggregator</module>
|
||||
<module>callback</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
|
Loading…
x
Reference in New Issue
Block a user