简体   繁体   English

数据隐藏是封装,但并非所有封装都是数据隐藏

[英]data hiding is encapsulation, but not all encapsulation is data hiding

I have read almost more than 100 links and explored all the questions on SO but :( still unable to understand 我已阅读了近100多个链接,并探讨了所有关于SO的问题但是:(仍然无法理解
The difference between Data Hiding and Encapsulation 数据隐藏和封装之间的区别
While reading this answer I read this line 在阅读这个答案时,我读了这一行
data hiding is encapsulation, but not all encapsulation is data hiding 数据隐藏是封装,但并非所有封装都是数据隐藏
So After all a huge research, I found that 所以经过一番巨大的研究,我发现了这一点
1) Data hiding is achieved by encapsulation OR it is a form of encapsulation(Am I Right)? 1)数据隐藏是通过封装实现的,或者它是一种封装形式(Am I Right)?
2) If yes, applying access specifier is data hiding (and Encapsulation too) But what is the mechanism which is only Encapsulation but Not Data Hiding ? 2)如果是,应用访问说明符是数据隐藏(和封装也是如此)但是什么是仅封装但不是数据隐藏的机制?

The short answer: 简短的回答:

1) Data hiding can be achieved without encapsulation, an example of this would be a private constant in a class, and that constant is not returned by any 'getter'. 1)数据隐藏可以在没有封装的情况下实现,其中一个示例是类中的私有常量,并且任何“getter”都不返回该常量。

2) Applying an access modifier might be data hiding and encapsulation. 2)应用访问修饰符可能是数据隐藏和封装。 You can achieve encapsulation but not data hiding, when you expose the data, but only to be modified by getters and setters. 在公开数据时,您可以实现封装,但不能实现数据隐藏,但只能由getter和setter修改。

And the long answer: 而答案很长:

Data hiding and encapsulation are quite different things, but related concepts. 数据隐藏和封装是完全不同的事情,但相关的概念。 Data hiding is about not leaking the implementation details any user of your class, while encapsulation is preventing unexpected changes on the data. 数据隐藏不会泄露任何类用户的实现细节,而封装可防止对数据进行意外更改。

The best explanation I've found of this is in the book `Growing Object Oriented Systems Guided by Tests" (page 49) 我发现的最好的解释是在“测试引导的面向对象增长系统”一书中(第49页)

What the authors say is that encapsulation is almost always a good thing, but data hiding can be in the wrong place, and they give the following example: 作者所说的是封装几乎总是一件好事,但数据隐藏可能在错误的地方,并且它们给出了以下示例:

  • Encapsulate the data structure for the cache in the Loader class 封装Loader类中缓存的数据结构
  • Encapsulate the name of the application's log file in the PrivacyPolicy class 在PrivacyPolicy类中封装应用程序日志文件的名称

Both of the above sound sensible, until we put them from the point of view of data hiding 以上两种声音都是明智的,直到我们从数据隐藏的角度来看它们

  • Hide the data structure for the cache in the Loader class 在Loader类中隐藏缓存的数据结构
  • Hide the name of the application's log file in the PrivacyPolicy class 在PrivacyPolicy类中隐藏应用程序日志文件的名称

In the example of the cache, it makes sense to hide it. 在缓存的示例中,隐藏它是有意义的。 But regarding the application log file name it doesn't make sense to hide it. 但是关于应用程序日志文件名,隐藏它是没有意义的。

Abstaction Abstaction

  • Focuses on essential aspects and hides background/implementation details 重点关注重要方面并隐藏背景/实施细节

  • Focuses on outside view of the object Example : Stack class [Abstaction focuses on the services provided by the class Push, Pop] 重点关注对象的外部视图示例:Stack类[Abstaction专注于类Push,Pop提供的服务]

  • Abstraction allows capturing the entire object behaviour no more no less. 抽象允许捕获整个对象行为,而不是更少。
  • It focuses on what object can do 它着重于对象可以做什么
  • It helps in identifying the classes based on the responsibility driven approach [Classifying the system as set of objects based on responsibility carried out by a class] 它有助于根据责任驱动的方法识别类[根据类的责任将系统分类为一组对象]

Encapsulation 封装

  • Bundeling state and behaviour of object into single unit Bundeling状态和对象的行为分为单一单位
  • Achieved by defining class [Identigying states and behaviours and putting these two things together into class] 通过定义类[识别状态和行为并将这两个事物放在一起]来实现
  • Enables keeping states and behaviour of objects together 允许将对象的状态和行为保持在一起
  • It does not hide implementation details its purpose is to identifying states and behaviours and keeping these things together 它没有隐藏实现细节,其目的是识别状态和行为并将这些事物保持在一起
  • It focuses on the inside view of the object while abstaction focuses on the outside view of the object 它侧重于对象的内部视图,而abstaction则侧重于对象的外部视图
  • Encapsulation helps implementing abstaction 封装有助于实现抽象

Information Hiding 信息隐藏

  • Encapsulation must allow only essential services to be revealed, Information hiding a concept related to the Encapsulation is required to hide the implementation details of an object 封装必须只允许显示基本服务,隐藏与封装相关的概念的信息需要隐藏对象的实现细节
  • Information hiding and Encapsulation are not same 信息隐藏和封装不一样

Encapsulation is the decision to identify which states and behaviours of objects are to be put together Whereas Information hiding is the decision on which of the encapsulated items to be revealed to the user and what not to be reaveled to the user 封装是决定将对象的哪些状态和行为放在一起的决定而信息隐藏是决定要向用户显示哪些封装的项目以及哪些不被重新分配给用户

Encapsulation hide complexity . 封装隐藏了复杂性 Like we are creating getter and setter.. 就像我们正在创造吸气剂和定型器..

But Data Hiding means hiding something and In java we can done using access modifiers ie 但是数据隐藏意味着隐藏某些内容而在java中我们可以使用访问修饰符来完成,即

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

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