added Callback pattern

This commit is contained in:
vehpsr
2015-03-26 22:47:04 +02:00
parent 33362d60ad
commit bf2df42c55
10 changed files with 140 additions and 2 deletions

BIN
callback/etc/callback.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View 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
View 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>

View 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);
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
/**
* Callback interface
*/
public interface Callback {
public void call();
}

View 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.");
}
}

View 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();
}

View File

@ -0,0 +1,12 @@
package com.iluwatar;
import org.junit.Test;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}