It is:
FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE
CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES
WITHOUT KNOWING IT
It sounds simple and logical. But actually it is easy to find some code in TargetProcess which breaks this principle.
There is always a temptation to write the following code in derived type :
public class B : A {
public override void DoSomething()
{
if (_some_condition)
{
base.DoSomething();
}
else
{
this.DoSomethingElse();
}
}
}
This {if} statement can be masked much more.
What is the problem of such hierarchy? You do not know how your classes will be used in future. And client of your classes is expecting that A class is a variety of B class. So he/she may write some code which will not work correctly with both A and B instances. And after all some ugly checks may appear :
public void Do(A a)
{
if (a is B )
//...... oh man!?
}
Or probably the code will be refactored and hierarchy will be broken.
The simple idea --- inheritance should be used only if it satisfies Liskov Substitution principle. Otherwise some other patterns should be used for code re-use.
Комментариев нет:
Отправить комментарий