mardi 12 mars 2019

How to do abstraction/design pattern when return types are different

public final class ExcelReader{
    /**
     * @param file File object of the excel file
     * @param sheetNumbers integer array holding the sheet offset/numbers. Note offset starts from 0. So 0 is first sheet.
     * @return List of Sheets, where each <Sheet> hold a list of Rows, where each <Row> holds a list of Columns
     */
    public List<List<List<String>>> read(File file, final int ... sheetNumbers){
        //return List<List<List<String>>>
    }
}

public final class CSVReader{
    /**
     * @param file File object of the excel file
     * @return List of Rows, where each <Row> holds a list of Columns
     */
    public List<List<String>> read(File file){
        //return List<List<String>>
    }
}

I may have an HTML Reader or some other reader in future

I was trying to use Strategy with factory design pattern with a common Interface

public interface FileReader {
    //Return type is different for different readers
    public <TODO> read(File file); 
}

but then realised the return types are different and who knows what return type the HTMLReader might have, probably even a custom object.

I am not sure how to wire up these things together agains an abstraction. Could someone please shed some light.

I did find this discussion Design Patterns - what to use when the return type is different but I was not able to understand how I can use it in my case.

Thank you for reading

Aucun commentaire:

Enregistrer un commentaire