c# - How can subclasses share behavior when one already derives from a different base class? -
i have 2 classes implement isomebehavior. want them share functionality. replace isomebehavior abstract class, somebehaviorbase. problem 1 of subclasses derives class, , other class isn’t software own. (this c#, multiple inheritance isn't option.) subclass, derives 3rd party class, has no implementation. derives 3rd party class, , implements isomebehavior, 3rd party class can treated same way other subclasses implement isomebehavior.
what i've done, moment, implement extension method on isomebehavior. consuming code can call method. problem approach want force calling code use extension method. can't remove somemethod() interface, because extension method has call it.
any ideas on how let 2 classes elegantly share same behavior, when 1 of them derives another, third party, class? note: strategy design pattern sounds makes sense here, pattern used when behavior varies among subclasses. behavior here doesn't vary; needs shared.
is there reason can't use composition instead of delegation implement class derives 3rd party class? delegate interface methods instance of third party class. way, if want add functionality can use common base class.
this won't work in cases object identity relevant, in many cases it's reasonable design.
Comments
Post a Comment