What can I do if the Liskov substitution principle is violated?

The Liskov replacement principle (LSP) states that if an object o1 is a type S and it can be replaced by an object o2 which is a type T without violating the initial behavior (s) of all its users, then S is a subtype of T.

A common example used to display an LSP violation is Rectangle and its derived type Square. The argument is that although intuitively the square seems to be a subtype of the Rectangle, there is some Square behavior that differs from the Rectangle. The conclusion is that Square cannot be a subtype of Rectangle LSP.

All the explanations that I have found are ending, and I think this is not useful. I want to know what should I do if I have this problem? Create an S, which is not a subtype of T, and then what? What solutions can I solve?

Can someone please enlighten me with the answer to the impending question?

edit .. Instead of giving an example here, I refer you to this article .

+4
source share
5 answers

You can use the 'HAS A or ' USES , if you can not establish a relationship of 'the IS A .

, , B A, B, A. , A B.

+2

" ++ 3rd Edition" , Addison Wesley May 2005. 6, . 32.

Meyers Rectangle Square, , .

:

" " - ". , , .

.

+1

LSP - "is-a", , LSP , . , - .

: LSP , , , "is-a" (, ), .

0

. .

, IS.

0

, LSP /, , IS_A -, IS_A .

IS_A - . , . , , . , IS_A, , .

0

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


All Articles