Does this solve the Liskov Substitution square-rectangle violation?

前端 未结 2 1740
离开以前
离开以前 2021-01-20 03:14

I\'m very new to the SOLID design principles. One thing I had problem with understanding is the \"Square-rectangle\" example of a Liskov Substition Principle violation. Why

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 03:55

    Imagine the user is implementing a bounding box in a GUI application, similar to this:

    enter image description here

    They want to represent this blue box by a Rectangle class, so that if the user clicks & drags down its height will increase; if the user drags right, its width will increase.

    LSP states that a client should be able to use a derived class (Square) wherever you would use its superclass (Rectangle) without breaking the business logic of Rectangle — i.e. a user should be able to sub in one for the other & the rest of their code shouldn't break.

    But the following are incompatible with each other:

    • It's an assumed post-condition of Rectangle that it's setter methods won't cause side effects (i.e. setWidth shouldn't affect the height)
    • It's inherent to the logic of a Square that its width will always equal its height.

    If the programmer used Square instead of Rectangle, their assumption above wouldn't work, as if the user dragged down, the box would get bigger horizontally & vertically at the same time.


    The trouble with the Square/Rectangle example is that we're assuming too much about Rectangle to begin with. A rectangle can have a different length to its height, but this is a property of a specific type of rectangle (an oblong rectangle).

    A square is a rectangle, but a square is not an oblong rectangle. If we want to assume the behaviour of an oblong about our Rectangle class (that it's width & height can differ), it's then doesn't make sense for our Square class to extend from that.

提交回复
热议问题