dimanche 16 décembre 2018

Deduce derived types polymorphically

Whats the recommended design approach/alternative to the situation below:

BaseCalculator:
BaseType prepareData()
useData(BaseType)

Derived calculators use derived type to override base functionality -

DerivedCalculator1:
BaseType prepareData(){ return DerivedType1}
useData(BaseType t1){ DerivedType1 t=(DerivedType1)t1 //typecast  down and proceed....}

DerivedCalculator2
BaseType prepareData(){ return DerivedType2}
useData(BaseType t1){ DerivedType2 t=(DerivedType2)t1 //typecast down and proceed....}

Is there a design approach to avoid typecasting by the derived classes - as it always leaves the gate open for a run-time mishap?

Aucun commentaire:

Enregistrer un commentaire