I have two classes that implement ISomeBehavior. Now I want them to share functionality. Normally, I would replace ISomeBehavior with an abstract class, such as SomeBehaviorBase. The problem is that one of the subclasses already comes from another class, and the other class is not the software that we own. (This is C #, so multiple inheritance is not an option.) A subclass that derives from a third-party class has no implementation. It simply stems from the third class and implements ISomeBehavior, so a third-party class can be handled in the same way as other subclasses that implement ISomeBehavior.
What I have done, at the moment, implements the extension method on ISomeBehavior. Now the consumption code can call the method. The problem with this approach is that I want to force the calling code to use this extension method. I cannot remove SomeMethod () from the interface because the extension method should eventually call it.
Any ideas on how two classes elegantly share the same behavior when one of them already comes from the other, third person, class? Note. The strategy design pattern sounds like it makes sense here, but this pattern is used when the behavior varies between subclasses. The behavior here does not change; it just needs to be exchanged.
c # subclass behavior share
Bob horn
source share