Skip to content

Instantly share code, notes, and snippets.

@vnnvanhuong
Last active July 15, 2017 04:14
Show Gist options
  • Save vnnvanhuong/c287fe2c8802414944be650d42b2a72a to your computer and use it in GitHub Desktop.
Save vnnvanhuong/c287fe2c8802414944be650d42b2a72a to your computer and use it in GitHub Desktop.
abstract class AbstractDocumentGenerator{
//THIS IS TEMPLATE METHOD
public final File generate() {
Document defaultDocument = prepareDocumentByDefault();
Document refinedDocument = redefineDocument(defaultDocument);
String templatePath = getTemplatePath();
String outputName = getOutputName();
return DocFactory.generate(refinedDocument, templatePath, outputName);
}
//This method is implemented at this class, can not be overridden
private Document prepareDocumentByDefault() {
Document document = new Document();
//TODO: prepare default here
return document;
}
//This is hook/default method which can be overridden by subclasses
protected Document redefineDocument(Document defaultDocument) {
return defaultDocument;
}
//These method must be implemented by subclass
protected abstract String getOutputName();
protected abstract String getTemplatePath();
}
class PaymentRequestDocumentGenerator extends AbstractDocumentGenerator {
@Override
protected String getOutputName() {
return "payment_request.pdf";
}
@Override
protected String getTemplatePath() {
return "/home/document_template/payment_request.docx";
}
}
class StandingOrderDocumentGenerator extends AbstractDocumentGenerator {
@Override
protected String getOutputName() {
return "standing_order.pdf";
}
@Override
protected String getTemplatePath() {
return "/home/document_template/standing_order.docx";
}
//Override the default method
@Override
protected Document redefineDocument(Document defaultDocument) {
defaultDocument.setDescription("Overriden the default");
return defaultDocument;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment