DocumentReader.java
import java.io.File;
public abstract class DocumentReader {
protected String fileName;
public DocumentReader(String fileName){
this.fileName=fileName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void processFile(){
boolean isPresent = checkforFilePresent();
if(!isPresent)
System.out.println("Could not find a file::"+fileName);
openFile();
readFile();
closeFile();
}
public boolean checkforFilePresent(){
System.out.println("checking if file is present::"+fileName);
return true;
}
public abstract void openFile();
public abstract void readFile();
public void closeFile(){
System.out.println("closing the file::"+fileName);
}
}
PDFDocumentReader.java
public class PDFDocumentReader extends DocumentReader{
public PDFDocumentReader(String fileName){
super(fileName);
}
public void openFile(){
System.out.println("opening the pdf file with Acrobat reader::"+fileName);
}
public void readFile(){
System.out.println("reading the pdf file with Acrobat reader::"+fileName);
}
}
WordDocumentReader.java
public class WordDocumentReader extends DocumentReader{
public WordDocumentReader(String fileName){
super(fileName);
}
public void openFile(){
System.out.println("opening the word file with MS office::"+fileName);
}
public void readFile(){
System.out.println("reading the word file with MS office::"+fileName);
}
}
TemplatePatternDemo.java
public class TemplatePatternDemo {
public static void main(String[] args) {
//read the ABC.PDF file
DocumentReader reader = new PDFDocumentReader("ABC.pdf");
reader.processFile();
//read XYZ.doc file
reader = new WordDocumentReader("XYZ.doc");
reader.processFile();
}
}
Output
