mercredi 13 décembre 2017

Design for type ambiguity

Looking for refactoring suggestions on the below scenario.
We have a DataMap<header,RecordHolder>.
The RecordHolder class has an approximate structure as follows.
RecordA,RecordB.... inherit from a common base type - Record.

RecordHolder{
    RecordA
    RecordB
    RecordC
    RecordD;

    setRecordA();
    setRecordB();
    ....
}

A parser utility reads each line of a file and translates into the corresponding record type :

Map<key,Record> parse(String line){....}

On return from parse,the RecordHolder instance corresponding to the key is to be fetched - and the appropriate setRecordXXX method has to be invoked to update the parsed record.

RecordHolder d = DataMap.get(key);
if(d==null){ ==========> (A)
    d = new RecorHolder();
    DataMap.put(key,d);
}
d.setRecordXXX(Record);=========>(B)

Questions at:
(A) Whats the best idiom for get-else-put-and-return for map.
(B) How can i achieve this without an explicit type check of Record(RecordA,RecordB etc etc)

Aucun commentaire:

Enregistrer un commentaire