通过信息隐藏进行有效封装的精彩示例?

发布于 2024-07-14 10:59:59 字数 257 浏览 10 评论 0原文

抽象封装是互补的概念:抽象侧重于对象的可观察行为……封装侧重于产生这种行为的实现。”封装通常是通过信息隐藏来实现的,这是隐藏对象所有对其基本特征没有贡献的秘密的过程。” - Grady Booch<。 /strong> 在《面向对象分析与设计》中

您能否向我展示一些令人信服的示例,说明通过信息隐藏进行封装的好处?

"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics." - Grady Booch in Object Oriented Analysis and Design

Can you show me some powerfully convincing examples of the benefits of encapsulation through information hiding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那伤。 2024-07-21 10:59:59

我的第一堂面向对象课程中给出的例子:

想象一个媒体播放器。 它抽象了播放、暂停、快进等概念。作为用户,您可以使用它来操作设备。

您的 VCR 实现了此接口并隐藏封装机械驱动器和磁带的详细信息。

当媒体播放器的新实现到来时(例如 DVD 播放器,它使用光盘而不是磁带),它可以替换媒体播放器中封装的实现,并且用户可以继续就像使用 VCR 一样使用它(相同的操作,例如播放、暂停等......)。

这就是通过抽象信息隐藏的概念。 它允许更改实现细节,而无需用户知道,并促进低耦合代码。

The example given in my first OO class:

Imagine a media player. It abstracts the concepts of playing, pausing, fast-forwarding, etc. As a user, you can use this to operate the device.

Your VCR implemented this interface and hid or encapsulated the details of the mechanical drives and tapes.

When a new implementation of a media player arrives (say a DVD player, which uses discs rather than tapes) it can replace the implementation encapsulated in the media player and users can continue to use it just as they did with their VCR (same operations such as play, pause, etc...).

This is the concept of information hiding through abstraction. It allows for implementation details to change without the users having to know and promotes low coupling of code.

扛起拖把扫天下 2024-07-21 10:59:59

*nix 将字符流(磁盘文件、管道、套接字、tty 等)抽象为单个实体(“一切都是文件”)模型,允许将各种工具应用于各种数据源/ 以一种如果没有封装就不可能实现的方式下沉。

同样,各种语言中的流概念,对列表、数组、文件等进行抽象。

此外,像数字这样的概念(对整数、六种浮点数、有理数等进行抽象)想象一下,如果更高级别的代码被赋予尾数格式等,并自行处理。

The *nix abstraction of character streams (disk files, pipes, sockets, ttys, etc.) into a single entity (the "everything is a file") model allows a wide range of tools to be applied to a wide range of data sources / sinks in a way that simply would not be possible without the encapsulation.

Likewise, the concept of streams in various languages, abstracting over lists, arrays, files, etc.

Also, concepts like numbers (abstracting over integers, half a dozen kinds of floats, rationals, etc.) imagine what a nightmare this would be if higher level code was given the mantissa format and so forth and left to fend for itself.

风筝在阴天搁浅。 2024-07-21 10:59:59

我知道已经有一个公认的答案,但我想再提出一个: OpenGL/DirectX

这些 API 都不是完整的实现(尽管 DirectX 在这方面肯定有点头重脚轻),而是通用的通信方法将命令渲染到显卡。

卡供应商为特定卡提供实现(驱动程序),在许多情况下,这是非常特定于硬件的,但作为用户,您永远不需要关心一个用户运行的是 GeForce ABC,另一个用户运行的是 Radeon XYZ,因为确切的实现隐藏在高级 API 后面。 如果不是这样,您需要在游戏中为您想要支持的市场上的每张卡设置一个代码路径,这从第一天起就完全无法管理。这种方法的另一个大优点是 Nvidia/ATI 可以发布一个他们的驱动程序更新、更高效的版本,您无需付出任何努力,就会自动受益。

同样的原理也适用于声音、网络、鼠标、键盘……基本上计算机的任何组件。 无论封装发生在硬件级别还是软件驱动程序中,在某些时候,所有设备细节都会被隐藏起来,以便您可以将任何键盘视为键盘,而不是 Microsoft Ergonomic Media Explorer Deluxe Revision 2当

你这样看时,很快就会发现,如果没有某种形式的封装/抽象,我们今天所知道的计算机根本无法工作。 这对你来说足够精彩吗?

I know there's already an accepted answer, but I wanted to throw one more out there: OpenGL/DirectX

Neither of these API's are full implementations (although DirectX is certainly a bit more top-heavy in that regard), but instead generic methods of communicating render commands to a graphics card.

The card vendors are the ones that provide the implementation (driver) for a specific card, which in many cases is very hardware specific, but you as the user need never care that one user is running a GeForce ABC and the other a Radeon XYZ because the exact implementation is hidden away behind the high-level API. Were it not, you would need to have a code path in your games for every card on the market that you wanted to support, which would be completely unmanageable from day 1. Another big plus to this approach is that Nvidia/ATI can release a newer, more efficient version of their drivers and you automatically benefit with no effort on your part.

The same principle is in effect for sound, network, mouse, keyboard... basically any component of your computer. Whether the encapsulation happens at the hardware level or in a software driver, at some point all of the device specifics are hidden away to allow you to treat any keyboard, for instance, as just a keyboard and not a Microsoft Ergonomic Media Explorer Deluxe Revision 2.

When you look at it that way, it quickly becomes apparent that without some form of encapsulation/abstraction computers as we know them today simply wouldn't work at all. Is that brilliant enough for you?

戏舞 2024-07-21 10:59:59

世界上几乎每个 Java、C# 和 C++ 代码库都具有信息隐藏:它就像类的 private: 部分一样简单。

外界看不到私有成员,因此开发人员可以更改它们,而无需担心其余代码无法编译。

Nearly every Java, C#, and C++ code base in the world has information hiding: It's as simple as the private: sections of the classes.

The outside world can't see the private members, so a developer can change them without needing to worry about the rest of the code not compiling.

饮惑 2024-07-21 10:59:59

什么? 你还不相信吗?

更容易展示相反的情况。 我们过去常常编写无法控制谁可以访问其实现细节的代码。 这使得有时几乎不可能确定哪些代码修改了变量。

此外,如果世界上的每一段代码都可能依赖于特定具体类的实现,那么您就无法真正抽象某些东西。

What? You're not convinced yet?

It's easier to show the opposite. We used to write code that had no control over who could access details of its implementation. That made it almost impossible at times to determine what code modified a variable.

Also, you can't really abstract something if every piece of code in the world might possibly have depended on the implementation of specific concrete classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文