The principle of derivation of inheritance and interface

Does the inheritance from the class inherit unused methods from the principle of interface segregation?

For instance:

abstract class Base { public void Receive(int n) { // . . . (some important work) OnMsg(n.ToString()); } protected abstract void OnMsg(string msg); } class Concrete : Base { protected override void OnMsg(string msg) { Console.WriteLine("Msg: " + msg); } } 

Concrete depends on the Base.Receive(int n) method, but it never uses it.

UPD

Definition I use:

ISP states that no client should depend on the methods that it is not used.

+6
source share
2 answers

I think you are misinterpreting what the principle of separation of segregation says. In your case, you are fine and do not "force" any implementation. In fact, you apply the template template template template

If you had a hypothetical

 interface ICommunicateInt { int Receive(); void Send(int n); } 

to implement it, your Base class would have to implement the Send method, which it does not need. So, your ISP suggests that it’s better to have:

 interface ISendInt { void Send(int n); } interface IReceiveInt { int Receive(); } 

so your classes can choose one or both. Also, methods in other classes that need a class that can send Int may require

 void Test(ISendInt snd) // void Test(ICommunicateInt snd) // Test would "force" snd to depend on // a method that it does not use 
+7
source

I do not see that Concrete "depends" on Base.Receive () in how I will use this term. How should Concrete change if the Recipient has been changed? I would say not at all. Suppose we replaced Receive () with a method of a different name or with a different signature, Concrete would not know. Does Concrete () get called? No. Therefore, I do not see a dependency on Receive () here.

The dependency is associated with the OnMsg () signature, Concrete is involved in a very specific relationship with the base, fulfilling this OnMsg () contract.

But in another sense, Concrete is very dependent on Base.Receive (), which is an interface to the outside world - without this method, no one can access Concrete capabilties. In this sense, Concrete "uses" Base.Receive () at a very fundamental level.

0
source

Source: https://habr.com/ru/post/949089/


All Articles