diff --git a/execute-around/pom.xml b/execute-around/pom.xml new file mode 100644 index 000000000..8d8862063 --- /dev/null +++ b/execute-around/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + execute-around + + + junit + junit + test + + + diff --git a/execute-around/src/main/java/com/iluwatar/App.java b/execute-around/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..d3db00045 --- /dev/null +++ b/execute-around/src/main/java/com/iluwatar/App.java @@ -0,0 +1,30 @@ +package com.iluwatar; + +import java.io.FileWriter; +import java.io.IOException; + +/** + * The Execute Around idiom specifies some code to be executed before and after + * a method. Typically the idiom is used when the API has methods to be executed in + * pairs, such as resource allocation/deallocation or lock acquisition/release. + * + * In this example, we have SimpleFileWriter class that opens and closes the file + * for the user. The user specifies only what to do with the file by providing the + * FileWriterAction implementation. + * + */ +public class App { + + public static void main( String[] args ) throws IOException { + + new SimpleFileWriter("testfile.txt", new FileWriterAction() { + + @Override + public void writeFile(FileWriter writer) throws IOException { + writer.write("Hello"); + writer.append(" "); + writer.append("there!"); + } + }); + } +} diff --git a/execute-around/src/main/java/com/iluwatar/FileWriterAction.java b/execute-around/src/main/java/com/iluwatar/FileWriterAction.java new file mode 100644 index 000000000..3f4fcd16c --- /dev/null +++ b/execute-around/src/main/java/com/iluwatar/FileWriterAction.java @@ -0,0 +1,15 @@ +package com.iluwatar; + +import java.io.FileWriter; +import java.io.IOException; + +/** + * + * Interface for specifying what to do with the file resource. + * + */ +public interface FileWriterAction { + + void writeFile(FileWriter writer) throws IOException; + +} diff --git a/execute-around/src/main/java/com/iluwatar/SimpleFileWriter.java b/execute-around/src/main/java/com/iluwatar/SimpleFileWriter.java new file mode 100644 index 000000000..c4be8a9fa --- /dev/null +++ b/execute-around/src/main/java/com/iluwatar/SimpleFileWriter.java @@ -0,0 +1,23 @@ +package com.iluwatar; + +import java.io.FileWriter; +import java.io.IOException; + +/** + * + * SimpleFileWriter handles opening and closing file for the user. The user + * only has to specify what to do with the file resource through FileWriterAction + * parameter. + * + */ +public class SimpleFileWriter { + + public SimpleFileWriter(String filename, FileWriterAction action) throws IOException { + FileWriter writer = new FileWriter(filename); + try { + action.writeFile(writer); + } finally { + writer.close(); + } + } +} diff --git a/execute-around/src/test/java/com/iluwatar/AppTest.java b/execute-around/src/test/java/com/iluwatar/AppTest.java new file mode 100644 index 000000000..3f256e7aa --- /dev/null +++ b/execute-around/src/test/java/com/iluwatar/AppTest.java @@ -0,0 +1,19 @@ +package com.iluwatar; + +import java.io.IOException; + +import org.junit.Test; + +/** + * + * Tests execute-around example. + * + */ +public class AppTest { + + @Test + public void test() throws IOException { + String[] args = {}; + App.main(args); + } +} diff --git a/pom.xml b/pom.xml index 7cfff29c4..5c15fe5f1 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ null-object event-aggregator callback + execute-around