Post-decorates a targeted object's methods.
After each execution of a targeted object's method, the same method of the
decorator object will be called. The
result (if any) from the invocation of the targeted object's method will be returned but you can choose to return the
decorator result if you want to.
Be aware:
The type of a targeted object should be an interface for this functionality to work
Any exception caused by an invocation of a
decorator object's method will result in disrupting the default
program's flow
Example: Assuming we have the following code:
interface IUploadFileService {
boolean upload(String file, String destination);
}
public class FileManager {
private IUploadFileService uploadFileService;
private static final String DEFAULT_DESTINATION = "http://example.org/default/destination/";
public void manage(String fileName) {
if( uploadFileService.upload(fileName, DEFAULT_DESTINATION) ) {
System.out.println("File "+fileName+" sent to "+DEFAULT_DESTINATION);
} else {
System.out.println("Unable to sent "+fileName+" to "+DEFAULT_DESTINATION);
}
}
}
Let's say we want to decorate the uploadFileService.upload(...) part, so that additional functionality is executed
before the actual uploadFileService.upload(...) logic, the following code will do the job:
IUploadFileService uploadFileServiceDecorator = ...;
FileManager fileManager = new FileManager();
field("uploadFileService").ofType(IUploadFileService.class)
.in(fileManager)
.postDecorateWith(uploadFileServiceDecorator);
However, if there is an exception when calling
uploadFileServiceDecorator.upload(fileName, DEFAULT_DESTINATION)
the default program's flow will be interrupted and the
uploadFileService.upload(fileName, DEFAULT_DESTINATION)
will not be executed.