vendredi 25 août 2017

How to approach strategy design in OOP. [migrated]

My class pseudo code is in the following format:

public class Config {
    final private Map<String, TableSchema> schemas;

    public Config() {
        schemas = parseSchemas();
    }
    public TableSchema getSchema(String tableName) {
        TableSchema tableSchema = schemas.get(tableName);
        if(/*Based on certain conditions*/) {
            return new TableSchema(/*Pass some values*/)
        }
        return tableSchema;
    }
    Map<String, TableSchema> parseSchemas() {
        //Read some config files 
        tableSchema = new TableSchema(/*Pass some values based on the read config files*/);
        schemas.put(tableName, tableSchema);
    }
}

public class DBOperations {
    private final TableSchema schema;
    public DBOperations(Config config, String tableName) {
        schema = config.getSchema(tableName);
    }
}

public class TableSchema {
    method1();
    method2();
}

To access the TableSchema methods in DBOperations, all I needed to do was call schema.method1() and so on. However now I need to have multiple implementations of DBOperations which may not all have the same TableSchema methods to operate on (function name might be the same, but implementation is different). For this I was thinking of making a TableSchema interface and have multiple implementations of it based on my requirements along the lines of strategy pattern.

However if I use the strategy pattern, I will need to pass the constructor as well. However the values of the class TableSchema depends on the values I retreive in the class Config. How should I approach my design in this case?

Aucun commentaire:

Enregistrer un commentaire