In C#, a class B serves as the only base class of three classes C, D and E.
B has a virtual method, which is overridden in C, D and E. C and D have the same implementation of the override method, different from the one in E.
C and D don't have other things in common.
How can I remove code duplicate between C and D?
Two ways that I am considering are:
-
create a new class
Fderived fromBand makeCandDderived fromFinstead ofB. Move the common override method inCandDtoF. I think it is overkill, sinceCandDdon't have other things in common. -
create a new method in
B, named differently from the virtual method, and implemented identically to the common override methods inCandD. Then change the implementations of the common override methods inCandD, to just a call the new method inB. Is this an elegant solution? Is it some design pattern or Object-Oriented trick?
Thanks.
I don't add C# as a tag, because I think the question is not C# specific, but general to all object oriented languages.
Aucun commentaire:
Enregistrer un commentaire