mardi 10 septembre 2019

Is it possible to solve the design issue with template?

There is three classes involved : a parent class A, and two children classes A1 and A2. I do not have access to the code of these methods as they are compiled in a DLL.

In the design of these classes, A1 and A2 share a set of common methods which are not defined in the base class A. Also these methods are not either gathered in an interface.

But it turns out that I have to surcharge a method Method(A1) with Method(A2) because the same logic is called at different places but with one and the other children classes A1 and A2.

I made a naïve attempt to "factorize" the code in a template method like Method(Type AT) but this does not work because even if the code is the same in both surcharges, within there is a couple of lines "AT.SharedMethod()" and the compilator complains because it has no guarantee that an object of type "Type" has a method "SharedMethod()".

Do you know if there is a way to avoid to dupplicate the code?

Remark : I would like to avoid the use of a switch like "if type of object is ... then ..."

Thanx

Aucun commentaire:

Enregistrer un commentaire