Hello everyone, I am han Cao 😈, a grass code ape 🐒 who has been working for more than a year. If you like my article, you can follow ➕ to like it and grow up with me. Add my wechat: Hancao97, invite you to join the group, learn and communicate together, and become a better engineer

“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

background

This is the eighth installment of my clean Architecture series, in which we will learn about the PRINCIPLE of Substitution in LSPS

Clean Architecture Series:

  • Hancao’s column on the Way to Neat architecture

LSP Richter replacement principle

In 1988, Barbara Liskov wrote in describing how to define subtypes:

What is needed here is a kind of substitutability: we can call S a subtype of T if for every object o1 of type S there exists an object O2 of type T such that the program P that operates on type T behaves the same when o1 is replaced by O2.

To understand the above statement, let me give you some examples:

  • Case a

Suppose we have a License class with a method called calcFee() that will be called by the Billing application. The License class has two “subtypes” : PersonalL Workcense and BusinessLicense, which use different algorithms to calculate License fees.

The above design is LSP compliant because the Billing application does not depend on any of the derived classes it uses. In other words, the objects of the two derived classes can be used to replace the License objects.

  • Case 2

The square/rectangle problem is a well-known example of an LSP design violation. In this case, the Square class is not a subtype of the Rectangle class, because the height and width of the Rectangle class can be changed separately, whereas the height and width of the Square class must be changed together. Because the User class always thinks it’s manipulating the Rectangle class, it can cause a few tears.

The only way to prevent this LSP violation is to add detection logic to the User class to distinguish between Rectangle and Square. However, the behavior of the User class would depend on the class it uses, and the two classes would not be interchangeable.

conclusion

In the early days of the object-oriented programming revolution, the common perception was that LSPS were just a way to guide how to use inheritance relationships, but over time, LSPS evolved into a broader design principle that guided interfaces and how they were implemented. LSP can and should be applied at the level of software architecture, because the system architecture has to add a lot of complex coping mechanisms when replaceable is violated.

The last

✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨

Youth never knows heaven and earth

Conceit and talent are everywhere you look

Pretentious as it is

I was honest

I love such a boy

Humble and arrogant

Proud and calm ☀️

✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨

Your likes and attention are my constant motivation, you can add my wechat: HancAO97, invite you to join the group, learn and communicate together, become a better front-end engineer ~