Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between liskov substitution principle and interface segregation principle

Is there any core difference between Liskov Substitution Principle (LSP) and Interface Segregation Principle (ISP)? Ultimately, both are vouching for designing the interface with common functionalities and introduce a new interface when you have special purpose of functionalities.

like image 983
Nilanjan Saha Avatar asked Feb 01 '19 13:02

Nilanjan Saha


People also ask

Does LSP apply to interfaces?

Ad 1): Yes. However, it is hard to make interfaces enforce the contracts, and they really should. Ad 2): Programming against an interface is a trade-off. As you point out interfaces encourages violation of LSP for all interfaces with a non-trivial contract.

What is interface segregation in SOLID principles?

In the field of software engineering, the interface segregation principle (ISP) states that no code should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.

What is LSP Liskov Substitution Principle and what are some examples of its use?

Simply put, the Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. In other words, what we want is to have the objects of our subclasses behaving the same way as the objects of our superclass.

Why it is called Liskov Substitution Principle?

Liskov's notion of a behavioural subtype defines a notion of substitutability for objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g. correctness).


1 Answers

LSP: The subtype must honor the contracts it promises.

ISP: The caller shouldn't depend on more of the base type's interface than it needs.

Where they fit: If you apply the ISP, you use only a slice of the receiver's full interface. But according to LSP, the receiver must still honor that slice.

If you fail to apply ISP, there can be a temptation to violate LSP. Because "this method doesn't matter, it won't actually be called."

like image 197
Jon Reid Avatar answered Sep 17 '22 22:09

Jon Reid