简体   繁体   中英

Liskov substitution principle (Uncle Bob book)

I read in Agile Principles Patterns and Practices in C# (Uncle Bob) book that, the presence of degenerate functions in derivatives is not always indicative of an LSP violation, but it's worth looking at them when they occur.

My question is, can someone give me an example when they don't violate LSP.

A good C# example, I think, is the various stream classes. The abstract Stream class defines a number of methods that may not apply to derived classes. I'm thinking specifically of the Can[...] methods, CanRead , CanSeek , CanTimeout , etc. On the face of it, they're degenerate methods in a derived class they don't apply to, but they are aspects of a stream that don't necessarily need to be implemented. A method that refers to the Stream abstract class can query CanRead , for example, and react accordingly based on the result, even if it's a degenerate implementation, as long as the degenerate implementation returns the right answer. In this way, a Stream implementation might have degenerate methods, but not violate LSP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM