里斯科夫替换_里斯科夫换人原则:用词不当?

2024-01-16 16:10

本文主要是介绍里斯科夫替换_里斯科夫换人原则:用词不当?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

里斯科夫替换

In an influential keynote address on data abstraction and class hierarchies at the OOPSLA 1987 programming language research conference, Barbara Liskov said the following: “What is wanted here is something like the following substitution property: If for each object o₁ of type S there is an object o₂ of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o₁ is substituted for o₂, then S is a subtype of T.” [1]

在上在OOPSLA 1987编程语言的研究会议数据抽象和类层次结构一个有影响力的主题演讲中,芭芭拉Liskov的下面说:“这到底是怎么想的东西像下面的替代属性:如果类型的每个对象O 1那儿是对象类型为T的o 2 ,这样对于用T定义的所有程序P,当用o 1代替o 2时,P的行为不变,则S是T的子类型。” [1]

This characterisation has since been widely known as the Liskov Substitution Principle (LSP).

此特征此后被广泛称为Liskov替代原理(LSP)。

Unfortunately, though, it has several issues.

不幸的是,它有几个问题。

Firstly, in its original formulation, it is too strong: we rarely want the behavior of a subclass to be identical to that of its superclass; substituting a subclass object for a superclass object is often done with the intent to change the program’s behavior, albeit in a way that maintains the program’s desirable properties.

首先,在其原始表述中,它太强大了:我们很少希望子类的行为与其父类的行为相同; 用子类对象代替超类对象通常是为了改变程序的行为,尽管这样做可以保持程序的期望属性。

Secondly, it makes no mention of specifications, so it invites an incorrect reading where the implementation of type S is compared to the implementation of type T. This is problematic for several reasons, one being that it does not support the common case where T is abstract and has no implementation.

其次,它没有提到的规格 ,所以邀请一个不正确的阅读,其中S型的实施相比,类型的实现 T.这是有几个原因存在问题,其中之一是,它不支持常见的情况,其中T是抽象,没有实现。

Thirdly, and most subtly, in the context of object-oriented imperative programming it is difficult to define precisely what it means to universally or existentially quantify over objects of a given type, or to substitute one object for another. [2]

第三,也是最微妙的是,在面向对象的命令式编程的上下文中,很难准确定义对给定类型的对象进行普遍或存在量化或将一个对象替换为另一个对象的含义。 [2]

Consider a program with an abstract superclass Bag and a concrete subclass Stack. In such a program, we are not substituting a Stack object for a Bag object; indeed, class Bag has no objects of its own. Instead, we are simply using a Stack object as a Bag object.

考虑一个具有抽象超类Bag和具体子类Stack的程序。 在这样的程序中,我们并没有用Stack对象代替Bag对象; 实际上,Bag类没有自己的对象。 相反,我们只是将Stack对象用作Bag对象。

In an interview in 2016, Liskov herself explains that what she presented in her keynote address was an “informal rule”, that Jeannette Wing later proposed that they “try to figure out precisely what this means”, which led to their joint publication on behavioral subtyping, and indeed that “technically, it’s called behavioral subtyping”. During the interview, she does not use substitution terminology to discuss the concepts. [3]

在2016年的一次采访中,利斯科夫本人解释说,她在主题演讲中提出的是“非正式规则”,珍妮特·温后来提出,他们“试图精确地找出这意味着什么”,这导致他们共同发表了关于行为子类型化,实际上是“在技术上,这被称为行为子类型化”。 在面试期间,她没有使用替代术语来讨论概念。 [3]

Image for post
Prof. Barbara Liskov
芭芭拉·里斯科夫(Barbara Liskov)教授

Indeed, in 1994 Liskov herself, together with her co-author Jeannette Wing, gave us the terminology we need to speak correctly about the semantic correctness of subclass relationships in object-oriented programming: behavioral subtyping.

的确,1994年,Liskov自己与她的合著者Jeannette Wing一起给了我们一个术语,我们需要正确地谈论面向对象编程中子类关系的语义正确性:行为子类型化。

Behavioral subtyping is the principle that properties that clients can prove using the specification of an object’s presumed type should hold even though the object is actually a member of a subtype of that type. [4]

行为子类型化是一个原则,即即使对象实际上是该类型的子类型的成员,客户端也可以使用该对象的假定类型的规范来证明其属性。 [4]

For example, consider a type Stack and a type Queue, that both have a put method to add an element and a get method to remove one. Suppose the documentation associated with these types specifies that type Stack’s methods shall behave as expected for stacks (i.e. they shall exhibit LIFO behavior), and that type Queue’s methods shall behave as expected for queues (i.e. they shall exhibit FIFO behavior). Suppose, now, that type Stack were declared as a subclass of type Queue. Most programming language compilers ignore documentation and perform only the checks that are necessary to preserve type safety. Since, for each method of type Queue, type Stack provides a method with a matching name and signature, this check would succeed. However, clients accessing a Stack object through a reference of type Queue would, based on Queue’s documentation, expect FIFO behavior but observe LIFO behavior, invalidating these clients’ correctness proofs and potentially leading to incorrect behavior of the program as a whole.

例如,考虑一个Stack类型和一个Queue类型,它们都有一个put方法添加一个元素和一个get方法删除一个元素。 假设与这些类型相关的文档指定了Stack类型的方法应表现出对堆栈的期望(即,它们应表现出LIFO行为),而Queue类型的方法应表现出对队列的预期(即,它们表现出FIFO行为)。 现在,假设将该类型Stack声明为Queue类型的子类。 大多数编程语言编译器都忽略文档,而仅执行保持类型安全所必需的检查。 由于对于Queue类型的每种方法,Stack类型都提供了具有匹配名称和签名的方法,因此此检查将成功。 但是,基于Queue的文档,通过Queue类型的引用访问Stack对象的客户端将期望FIFO行为,但会观察到LIFO行为,从而使这些客户端的正确性证明无效,并有可能导致整个程序的错误行为。

This example violates behavioral subtyping because type Stack is not a behavioral subtype of type Queue: it is not the case that the behaviors allowed by the specification of Stack are also allowed by the specification of Queue.

此示例违反了行为子类型,因为类型Stack不是Queue类型的行为子类型:并非Queue规范也允许Stack规范允许的行为。

In contrast, a program where both Stack and Queue are subclasses of a type Bag, whose specification for get is merely that it removes some element, does satisfy behavioral subtyping and allows clients to safely reason about correctness based on the presumed types of the objects they interact with. Indeed, any object that satisfies the Stack or Queue specification also satisfies the Bag specification.

相比之下,其中Stack和Queue都是Bag类型的子类的程序,其获取的规范仅仅是删除了某些元素,满足了行为子类型,并允许客户端基于他们所假定的对象类型来安全地推断正确性。与。。。相互作用。 实际上,任何满足Stack或Queue规范的对象也都满足Bag规范。

It is important to stress that whether a type S is a behavioral subtype of a type T depends only on the specification of type T; the implementation of type T, if it has any, is completely irrelevant to this question. Indeed, type T need not even have an implementation; it might be a purely abstract class. As another case in point, type Stack above is a behavioral subtype of type Bag even if type Bag’s implementation exhibits FIFO behavior: what matters is that type Bag’s specification does not specify which element is removed by method get. This also means that behavioral subtyping can be discussed only with respect to a particular (behavioral) specification for each type involved, and that if the types involved have no well-defined behavioral specification, behavioral subtyping cannot be discussed meaningfully.

必须强调的是,类型S是否是类型T的行为子类型,仅取决于类型T的规范 。 T类型的实现 (如果有的话)与这个问题完全无关。 实际上,类型T甚至不需要实现。 它可能是一个纯粹的抽象类。 作为另一种情况,即使类型Bag的实现表现出FIFO行为,上面的Stack类型也是Bag类型的行为子类型:重要的是类型Bag的规范未指定方法get删除了哪个元素。 这也意味着只能针对所涉及的每种类型的特定(行为)规范来讨论行为子类型,并且如果所涉及的类型没有明确定义的行为规范,则不能有意义地讨论行为子类型。

In conclusion, I hope that I have convinced you that the correct terminology to use when discussing the correctness of subclass relationships in object-oriented programming is behavioral subtyping, not substitutability.

总之,我希望我已经说服了您,在讨论面向对象编程中子类关系的正确性时使用的正确术语是行为子类型,而不是可替代性。

翻译自: https://medium.com/@bart.jacobs/liskov-substitution-principle-a-misnomer-3a891c29d359

里斯科夫替换


http://www.taodudu.cc/news/show-8426187.html

相关文章:

  • 用词不当
  • 金融行业错别字识别,让专业术语更加准确无误
  • 渗透测试No.23 wave信息隐藏 look命令提权 HackSudo-FOG
  • 靶场练习No.1 Vulnhub靶场JANGOW01
  • 桂林旅游攻略
  • 2021年质量员-市政方向-通用基础(质量员)考试试卷及质量员-市政方向-通用基础(质量员)理论考试
  • 2021年塔式起重机司机考试及塔式起重机司机实操考试视频
  • 2021年流动式起重机司机考试题库及流动式起重机司机试题及解析
  • 2020装饰方向-岗位技能(施工员)模拟考试题及装饰方向-岗位技能(施工员)理论考试
  • 2021-08-07 2021年升降机司机最新解析及升降机司机考试试题
  • 对SOA的需求来达成工厂运营的灵活性和稳定性
  • 机械基础——教学(下学期)
  • 专题导读:大数据的系统结构
  • 实施网络可信身份战略符合技术发展方向,意义深远
  • 闪存绽放加速企业创新 2017中国闪存峰会在京召开
  • 2019年度CCF杰出演讲者名单今日出炉,中学教师上榜
  • CCF评选出2019年度杰出演讲者
  • 信息与数据科学国际会议——与众位大咖共商信息与数据科学发展的新未来!...
  • 清华计算机系舒继武 CCF-ADL 讲习班上篇:闪存存储系统的软件
  • 基于GA-BP遗传算法优化BP神经网络多维时序预测研究(Matlab代码实现)
  • 正则替换多个换行符为1个
  • QString替换多个字符
  • webStrom中的批量替换
  • python字符串一次替换多个字符使用replace替换多处
  • K3CLOUD联产品与副产品的定义与区分
  • Netty—NIO万字详解
  • Netty—Reactor线程模型详解
  • 生信基础-wsl+conda+mamba
  • 出版基础知识 | 出版历史知识
  • 13-高并发-连接池线程池详解
  • 这篇关于里斯科夫替换_里斯科夫换人原则:用词不当?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/613155

    相关文章

    C#实现添加/替换/提取或删除Excel中的图片

    《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

    Java操作xls替换文本或图片的功能实现

    《Java操作xls替换文本或图片的功能实现》这篇文章主要给大家介绍了关于Java操作xls替换文本或图片功能实现的相关资料,文中通过示例代码讲解了文件上传、文件处理和Excel文件生成,需要的朋友可... 目录准备xls模板文件:template.xls准备需要替换的图片和数据功能实现包声明与导入类声明与

    JVM内存调优原则及几种JVM内存调优方法

    JVM内存调优原则及几种JVM内存调优方法 1、堆大小设置。 2、回收器选择。   1、在对JVM内存调优的时候不能只看操作系统级别Java进程所占用的内存,这个数值不能准确的反应堆内存的真实占用情况,因为GC过后这个值是不会变化的,因此内存调优的时候要更多地使用JDK提供的内存查看工具,比如JConsole和Java VisualVM。   2、对JVM内存的系统级的调优主要的目的是减少

    notepad++ 正则表达式多条件查找替换

    基础语法参考: https://www.cnblogs.com/winstonet/p/10635043.html https://www.linuxidc.com/Linux/2019-05/158701.htm   通常情况下我们查找的内容和要被替换掉的内容是一样的,我们只需要使用正则表达式精确框定查找内容,替换直接输入要替换的内容即可。 但有时会比较复杂,查找的内容,只需要替换其中

    shell脚本中变量中字符串替换的测试 /和//的区别

    test_char=abbbcbbbf echo "bf:test_char = " $test_char test_char=${test_char/bbb/ddd} echo "af:test_char = " $test_char 输出: bf:test_char =  abbbcbbbf af:test_char =  adddcbbbf 只匹配第一个

    springboot启动时替换配置参数

    SpringBoot启动时配置参数替换 一.背景 SpringBoot项目启动的时候,在不使用配置中心等的前提下或者有公司强制使用指定的“密码箱”情况下,需要远程获取关键配置信息,比如数据库密码,则需要在项目启动前获取配置并且进行本地配置替换。 二.Demo实现 1.maven依赖 <dependencies><dependency><groupId>org.springframewor

    水处理过滤器运行特性及选择原则浅谈

    过滤属于流体的净化过程中不可缺的处理环节,主要用于去除流体中的颗粒物或其他悬浮物。水处理过滤器的原理是利用有孔介质,从流体中去除污染物,使流体达到所需的洁净度水平。         水处理过滤器的滤壁是有一定厚度的,也就是说过滤器材具有深度,以“弯曲通 道”的形式对去除污染物起到了辅助作用。过滤器是除去液体中少量固体颗粒的设备,当流体进入置有一定规格滤网的滤筒后,其杂质被阻挡,而

    Java中等题-整数替换(力扣)

    给定一个正整数 n ,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n 。如果 n 是奇数,则可以用 n + 1或n - 1替换 n 。 返回 n 变为 1 所需的 最小替换次数 。 示例 1: 输入:n = 8输出:3解释:8 -> 4 -> 2 -> 1 示例 2: 输入:n = 7输出:4解释:7 -> 8 -> 4 -> 2 -> 1或 7 ->

    SpringBoot 集成 SpirePDF 实现文本替换

    SpirePDF 10.6.2 很强大,API 也封装的很好,使用的时候及其舒适。但是需要购买许可,不然有很大限制,最大的问题在于会添加水印,这就导致基本上用不了。有钱真好,真是嘴馋。 好在 SpirePDF 也有版本较老的免费版本,有查到一个 5.1.0。接下来附上使用代码 1、在 pom.xml 文件中添加他们的源 <!-- 使用 huawei / aliyun 的 Maven 源,提升

    NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001

    由于需要对NFI进行汉化,以及二次开发,首先要下载源码以后编辑通过,NIFI的源码,项目非常多,编译过程中需要编译超过570个jar包,同时编译过程很慢需要30多分钟. 1.首先下载NIFI源码,根据需要下载对应版本: https://github.com/kemixkoo/orchsym-runtime/   首先介绍一下,这个是一个公司根据nifi进行定制开发的,已经汉化,但是不能商