Skip to content

Commit 027fd99

Browse files
committed
Added example code for Execute Around idiom.
1 parent eee2160 commit 027fd99

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

execute-around/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>execute-around</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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
* The Execute Around idiom specifies some code to be executed before and after
8+
* a method. Typically the idiom is used when the API has methods to be executed in
9+
* pairs, such as resource allocation/deallocation or lock acquisition/release.
10+
*
11+
* In this example, we have SimpleFileWriter class that opens and closes the file
12+
* for the user. The user specifies only what to do with the file by providing the
13+
* FileWriterAction implementation.
14+
*
15+
*/
16+
public class App {
17+
18+
public static void main( String[] args ) throws IOException {
19+
20+
new SimpleFileWriter("testfile.txt", new FileWriterAction() {
21+
22+
@Override
23+
public void writeFile(FileWriter writer) throws IOException {
24+
writer.write("Hello");
25+
writer.append(" ");
26+
writer.append("there!");
27+
}
28+
});
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* Interface for specifying what to do with the file resource.
9+
*
10+
*/
11+
public interface FileWriterAction {
12+
13+
void writeFile(FileWriter writer) throws IOException;
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* SimpleFileWriter handles opening and closing file for the user. The user
9+
* only has to specify what to do with the file resource through FileWriterAction
10+
* parameter.
11+
*
12+
*/
13+
public class SimpleFileWriter {
14+
15+
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
16+
FileWriter writer = new FileWriter(filename);
17+
try {
18+
action.writeFile(writer);
19+
} finally {
20+
writer.close();
21+
}
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
*
9+
* Tests execute-around example.
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() throws IOException {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>null-object</module>
4242
<module>event-aggregator</module>
4343
<module>callback</module>
44+
<module>execute-around</module>
4445
</modules>
4546

4647
<dependencyManagement>

0 commit comments

Comments
 (0)