lundi 31 octobre 2022

Design pattern for file reader?

I am creating a module that reads CSV and Excel files using Apache POI and Opencsv.

For reading CSV files, I am creating 1 class and 2 methods:

class CsvReader {
  void open() {//implementation}
  List<CsvDto1> get1() {//implementation}
  List<CsvDto2> get2() {//implementation}
  void close() {//implementation}
}

For reading Excel files, I am also creating 1 class and 2 methods:

class ExcelReader {
  void open() {//implementation}
  List<ExlDto1> get3() {//implementation}
  List<ExlDto2> get4() {//implementation}
  void close() {//implementation}
}

All I want is to implement a pattern that will be helped maintainable in the future. So I created an interface called FileReadable:

interface FileReadable {
  void open();
  List<CsvDto1> get1()
  List<CsvDto2> get2()
  List<ExlDto1> get3()
  List<ExlDto2> get4()
  void close();
}

then CsvReader and ExcelReader will be implemented from FileRedable. The issue is get1() and get2() exist in CsvReader but they do not exist in ExcelReader, get3() and get4() exist in ExcelReader but they do not exist in CsvReader. How do I create a common read method for both classes or do we have any design pattern for this case?

   interface FileReadable {
      void open();
      Reader read();
      void close();
   }

Aucun commentaire:

Enregistrer un commentaire