2015-07-24 11:32:22 +03:00
|
|
|
package com.iluwatar.execute.around;
|
2015-03-29 21:25:13 +03:00
|
|
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
2015-11-01 21:29:13 -05:00
|
|
|
* 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.
|
2015-08-18 22:39:03 +03:00
|
|
|
* <p>
|
2015-11-01 21:29:13 -05:00
|
|
|
* In this example, we have {@link SimpleFileWriter} class that opens and closes the file for the
|
|
|
|
* user. The user specifies only what to do with the file by providing the {@link FileWriterAction}
|
|
|
|
* implementation.
|
2015-03-29 21:25:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class App {
|
|
|
|
|
2015-11-01 21:29:13 -05:00
|
|
|
/**
|
|
|
|
* Program entry point
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) throws IOException {
|
2015-03-29 21:25:13 +03:00
|
|
|
|
2015-11-01 21:29:13 -05:00
|
|
|
new SimpleFileWriter("testfile.txt", new FileWriterAction() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeFile(FileWriter writer) throws IOException {
|
|
|
|
writer.write("Hello");
|
|
|
|
writer.append(" ");
|
|
|
|
writer.append("there!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-29 21:25:13 +03:00
|
|
|
}
|