File tree Expand file tree Collapse file tree 6 files changed +106
-0
lines changed
Expand file tree Collapse file tree 6 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 4141 <module >null-object</module >
4242 <module >event-aggregator</module >
4343 <module >callback</module >
44+ <module >execute-around</module >
4445 </modules >
4546
4647 <dependencyManagement >
You can’t perform that action at this time.
0 commit comments