简体   繁体   English

Liskov 替换原则(鲍勃大叔书)

[英]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.我在 C# 中的敏捷原则模式和实践(鲍勃叔叔)一书中读到,导数中退化函数的存在并不总是表明违反 LSP,但是当它们发生时值得一看。

My question is, can someone give me an example when they don't violate LSP.我的问题是,有人可以举个例子,当他们不违反 LSP 时。

A good C# example, I think, is the various stream classes.我认为,一个很好的 C# 示例是各种流类。 The abstract Stream class defines a number of methods that may not apply to derived classes.抽象Stream类定义了许多可能不适用于派生类的方法。 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.我正在特别考虑Can[...]方法、 CanReadCanSeekCanTimeout等。从表面CanTimeout ,它们是不适用的派生类中的退化方法,但它们是不一定需要实现的流。 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.例如,引用Stream抽象类的方法可以查询CanRead ,并根据结果做出相应的反应,即使它是退化的实现,只要退化的实现返回正确的答案。 In this way, a Stream implementation might have degenerate methods, but not violate LSP.这样, Stream实现可能具有退化方法,但不会违反 LSP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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