Added example code for Execute Around idiom.
This commit is contained in:
		
							
								
								
									
										18
									
								
								execute-around/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								execute-around/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
<?xml version="1.0"?>
 | 
			
		||||
<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"
 | 
			
		||||
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 | 
			
		||||
  <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
  <parent>
 | 
			
		||||
    <groupId>com.iluwatar</groupId>
 | 
			
		||||
    <artifactId>java-design-patterns</artifactId>
 | 
			
		||||
    <version>1.0-SNAPSHOT</version>
 | 
			
		||||
  </parent>
 | 
			
		||||
  <artifactId>execute-around</artifactId>
 | 
			
		||||
  <dependencies>
 | 
			
		||||
    <dependency>
 | 
			
		||||
      <groupId>junit</groupId>
 | 
			
		||||
      <artifactId>junit</artifactId>
 | 
			
		||||
      <scope>test</scope>
 | 
			
		||||
    </dependency>
 | 
			
		||||
  </dependencies>
 | 
			
		||||
</project>
 | 
			
		||||
							
								
								
									
										30
									
								
								execute-around/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								execute-around/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							@@ -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!");
 | 
			
		||||
			}
 | 
			
		||||
    	});
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@@ -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();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								execute-around/src/test/java/com/iluwatar/AppTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								execute-around/src/test/java/com/iluwatar/AppTest.java
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user