Skip to content

Instantly share code, notes, and snippets.

@kaansonmezoz
Created December 19, 2019 22:11
Show Gist options
  • Save kaansonmezoz/739da0c9a185b542b5fd9aa3d23069cc to your computer and use it in GitHub Desktop.
Save kaansonmezoz/739da0c9a185b542b5fd9aa3d23069cc to your computer and use it in GitHub Desktop.
Factory Pattern ~ A Modern Approach - 3
public class CsvReader implements FileReader {
// some code here
public String readFile(String filePath) {
// ...
return fileContent;
}
}
public class ExcelReader implements FileReader {
// some code here
public String readFile(String filePath) {
// ...
return fileContent;
}
}
public class FileOperations {
// some code here
public String readFile(String filePath){
FileReader reader;
if(isPdfFile(filePath)){
reader = new PdfReader();
}
else if(isExcelFile(filePath)) {
reader = new ExcelReader();
}
else if(isCsvFile(filePath)){
reader = new CsvReader();
}
else {
throw new UnsupportedFileExtension("...");
}
return reader.readfile(filePath)
}
private String readPdfFile(String filePath) {
// some code here
return fileContent;
}
private String readExcelFile(String filePath) {
// some code here
return fileContent;
}
}
public interface FileReader{
public String readFile(String filePath);
}
public class PdfReader implements FileReader {
// some code here
public String readFile(String filePath) {
// ...
return fileContent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment