Skip to content

Instantly share code, notes, and snippets.

@kaansonmezoz
Created December 19, 2019 22:31
Show Gist options
  • Save kaansonmezoz/80cd0dad35eda76f2cb622faa82c5568 to your computer and use it in GitHub Desktop.
Save kaansonmezoz/80cd0dad35eda76f2cb622faa82c5568 to your computer and use it in GitHub Desktop.
Factory Pattern ~ A Modern Approach - 3
public class FileOperations {
private FileReaderFactory readerFactory;
// some code here
public String readFile(String filePath){
return readerFactory.createFileReader(filePath).readFile(FilePath);
}
}
public class FileReaderFactory {
// other necessary methods are here
public FileReader createFileReader(String filePath) {
FileReader reader;
if (isExcelFile(filePath)) {
reader = new ExcelReader();
}
else if(isPdfFile(filePath)) {
reader = new PdfReader();
}
else if(isCsvFile(filePath)) {
reader = new CsvReader();
}
else {
throw new UnsupportedFileException("...");
}
return reader;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment