#108 Consistent package naming throughout the examples

This commit is contained in:
Ilkka Seppala
2015-07-24 11:32:22 +03:00
parent af92d8dde5
commit 3d488ec15a
128 changed files with 963 additions and 873 deletions

View File

@@ -0,0 +1,30 @@
package com.iluwatar.execute.around;
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!");
}
});
}
}

View File

@@ -0,0 +1,15 @@
package com.iluwatar.execute.around;
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;
}

View File

@@ -0,0 +1,23 @@
package com.iluwatar.execute.around;
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();
}
}
}