“封装”是否有助于并行开发多个模块? [英] Does 'Encapsulation' help develop multiple modules parallely?

查看:123
本文介绍了“封装”是否有助于并行开发多个模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过SO问题后,我了解到

After going through SO questions, I learnt that,

封装是关于保护不变式并隐藏实现细节。

Encapsulation is about protecting invariants and hiding implementation details.

抽象与将接口实现分开

来自教室java 培训,据我了解,封装具有以下优势,

From class room java training, I learnt that, Encapsulation has following advantages,


为什么封装是您的朋友?

Why encapsulation is your friend?

[1]实现独立于功能。拥有接口文档的程序员
可以独立实现模块或ADT的新版本
。更好的新实现可以用
代替旧的实现。

[1] The implementation is independent of the functionality. A programmer who has the documentation of the interface can implement a new version of the module or ADT independently. A new, better implementation can replace an old one.

[2]封装可防止Doug编写破坏
模块内部数据的应用程序。在实际编程中,封装
减少了调试时间。很多。

[2] Encapsulation prevents Doug from writing applications that corrupt a module’s internal data. In real-world programming, encapsulation reduces debugging time. A lot.

[3] ADT可以保证保留其不变式。

[3] ADTs can guarantee that their invariants are preserved.

[4]团队合作。严格定义了模块之间的接口后,每个程序员
即可独立实现一个模块,而无需
访问其他模块。大型复杂的编程项目可以将
分解成几十个部分。

[4] Teamwork. Once you’ve rigorously defined interfaces between modules, each programmer can independently implement a module without having access to the other modules. A large, complex programming project can be broken up into dozens of pieces.

[5]文档和可维护性。通过定义明确的
接口,您可以使其他程序员更容易修复
离开公司数年后出现的错误。许多错误是由于模块之间
发生不可预见的交互作用所致。如果每个接口和每个模块的行为都有明确的
规范,则错误
更容易跟踪。

[5] Documentation and maintainability. By defining an unambiguous interface, you make it easier for other programmers to fix bugs that arise years after you’ve left the company. Many bugs are a result of unforeseen interactions between modules. If there’s a clear specification of each interface and each module’s behavior, bugs are easier to trace.

[6]

问题1:

Wrt Point1 (上方)说: 更好的新实现可以替换旧的实现。。这是抽象的目标,而不是封装。我对吗?

Wrt Point1(above) says, "A new, better implementation can replace an old one.". This is the goal of abstraction but not encapsulation. Am I correct?

问题2:

Wrt 第4点(上面) ,封装如何帮助程序员独立实现模块而无需访问其他模块?模块的并行实现与 Encapsulation 有什么关系?因为封装是关于保护 invariant 。这个答案也支持我的论点

Wrt Point 4(above), How Encapsulation help programmer to independently implement module without having access to other modules? How does parallel implementation of modules has anything to do with Encapsulation? Because Encapsulation is about protecting in-variants. This answer also supports my argument

推荐答案

要回答这个问题,我们必须弄清抽象和封装的含义。

To answer this question, we must clarify what we mean by "abstraction" and "encapsulation".

维基百科定义抽象如下:


从主要意义上讲,抽象是一个概念过程,通过该过程可以从特定示例的使用和分类(文字( 实际或具体)指称,第一原则或其他方法。 抽象是此过程的产物,该概念充当所有从属概念的超分类名词,并将所有相关概念连接为一个组,字段或类别。

Abstraction in its main sense is a conceptual process by which general rules and concepts are derived from the usage and classification of specific examples, literal ("real" or "concrete") signifiers, first principles, or other methods. "An abstraction" is the product of this process—a concept that acts as a super-categorical noun for all subordinate concepts, and connects any related concepts as a group, field, or category.

用于数学


数学抽象是提取数学概念内在本质的过程,消除了对它本来可能对现实世界对象的依赖连接,并将其泛化,使其具有更广泛的应用或与其他等效现象的抽象描述相匹配

Abstraction in mathematics is the process of extracting the underlying essence of a mathematical concept, removing any dependence on real world objects with which it might originally have been connected, and generalizing it so that it has wider applications or matching among other abstract descriptions of equivalent phenomena

计算机科学


<在计算机科学中,抽象是一种用于管理计算机系统复杂性的技术。它通过建立一个人与系统进行交互的复杂性级别来工作,将更复杂的细节抑制在当前级别以下。

In computer science, abstraction is a technique for managing complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level.

总而言之,抽象是概括的过程,抽象是该过程的结果。

In summary, abstraction is the process of generalization, and abstractions are the results of this process.

通过概括一些内容,我们使其更广泛地适用(可重用)。例如,如果我有一个对人进行分类的方法和一个对猫进行分类的方法,那么我可以推广一种对任何可以成对比较的事物进行分类的方法。
此方法有许多应用程序。这是有用的抽象。

By generalizing something, we make it more widely applicable (reusable). For instance, if I have a method to sort persons, and a method to sort cats, I may generalize a method to sort anything that can be pairwise compared. This method has numerous applications. It is a useful abstraction.

通过概括某些事物,我们将整个事物视为相同,而不再关心我们正在考虑的具体事物。在上面的示例中,我在要排序的对象上提取了 ,并在其内部表示中提取了 away

By generalizing something, we treat an entire group of things the same, and no longer care which concrete thing we are thinking about. In the example above, I have abstracted over the kinds of objects to be sorted, and abstracted away their internal representation.

因此,抽象与信息隐藏概念紧密相关,Wikipedia 定义,如下所示:

Abstraction is therefore closely related to the concept of information hiding, which Wikipedia defines as follows:


在计算机科学中,信息隐藏是原则计算机程序中最有可能更改设计决策的隔离,因此,如果更改了设计决策,则可以保护程序的其他部分免于大量修改。保护涉及提供一个稳定的接口,该接口可保护程序的其余部分免于实现(最有可能更改的细节)。

In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change).

换句话说,信息隐藏是使用编程语言功能(例如私有变量)或显式导出策略来防止类或软件组件的某些方面对其客户端的访问的能力。

Written another way, information hiding is the ability to prevent certain aspects of a class or software component from being accessible to its clients, using either programming language features (like private variables) or an explicit exporting policy.

也就是说,信息隐藏是一种强制抽象的方法。我们不仅允许调用者进行抽象思考,还通过隐藏他们不应该考虑的具体事物的知识来迫使他们这样做。

That is, information hiding is a way to enforce abstractions. We don't just permit our callers to think in an abstraction, we force them to, by hiding the knowledge of the concrete things they should not think about.

,我们终于可以讨论封装了,维基百科定义如下:

With that, we can finally talk about encapsulation, which Wikipedia defines as follows:


封装是将数据和函数打包为单个组件。大多数其他面向对象的编程语言都使用类来支持封装的功能,尽管也存在其他替代方法。通过构建难以穿透的墙来保护代码免受意外损坏,它可以选择性地隐藏对象中的属性和方法。

Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.

封装是一种信息隐藏(我们隐藏字段或方法)。

That is, encapsulation is a kind of information hiding (we hide fields or methods).

请牢记这一理论,问题:

With that theory in mind, on to your questions:


Wrt Point1(上方)说:新的,更好的实现可以代替旧的。这是抽象而不是封装的目标。我是正确的吗?

Wrt Point1(above) says, "A new, better implementation can replace an old one.". This is the goal of abstraction but not encapsulation. Am I correct?

由于封装可用于强制执行抽象,因此可以促进抽象的使用,从而有助于获取其利益。实际上,这就是封装的目的。

Since encapsulation can be used to enforce abstractions, it can promote their use, and can therefore contribute to reaping their benefit. In fact, this is the purpose of encapsulation.

但是,您正确地认为没有抽象的封装不会促进实现交换。这种封装未能实现其目的。

However, you are correct that encapsulation without abstraction does not promote implementation exchange. Such encapsulation has failed to achieve its purpose; it has been misused.

也就是说,您引用的文章默认假定正确使用了封装。

That is, the article you quote tacitly assumes that encapsulation has been used correctly.


封装如何帮助程序员独立实现模块而无需访问其他模块?模块的这种并行实现与封装有何关系?因为封装是关于保护不变式的。

How Encapsulation help programmer to independently implement module without having access to other modules? How this parallel implementation of modules has anything to do with Encapsulation? Because Encapsulation is about protecting in-variants.

如果不变式是通过封装来保护的,则调用代码可能不需要知道这个不变式。如果是这样,则该不变量已被抽象掉,调用代码的开发不受该不变量的影响,即另一个模块可以独立于该不变量而开发。

If invariants are protected through encapsulation, calling code may not need not be aware of this invariant. If so, the invariant has been abstracted away, the development of calling code is not affected by this invariant, i.e. the other module may be developed independent of that invariant.

这篇关于“封装”是否有助于并行开发多个模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆