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

问题描述

经过 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,

为什么封装是你的朋友?

[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] 当你的项目不工作时,会更容易找出哪个责怪队友.

[6] When your Project doesn’t work, it will be easier to figure out which teammate to blame.

问题 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 Point 4(以上),封装如何帮助程序员独立实现模块而无需访问其他模块?模块的并行实现与封装有什么关系?因为封装是关于保护in-variants.这个答案也支持我的论点

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".

维基百科定义抽象如下:

抽象在其主要意义上是一个概念过程,通过该过程,一般规则和概念从具体示例、字面(真实"或具体")能指、第一原理或其他方法的使用和分类中推导出来.抽象"是这个过程的产物——一个概念,它充当所有从属概念的超范畴名词,并将任何相关概念连接为一个组、领域或范畴.

数学:

数学中的抽象是提取数学概念的潜在本质的过程,消除对其最初可能与之相关的现实世界对象的任何依赖,并将其概括为具有更广泛的应用或与其他抽象描述相匹配等价现象

计算机科学:

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

综上所述,抽象是泛化的过程,抽象是这个过程的结果.

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.

通过概括某事,我们将一整组事物一视同仁,不再关心我们正在考虑哪个具体事物.在上面的例子中,我抽象了over要排序的对象的种类,并抽象了去掉它们的内部表示.

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.

因此抽象与信息隐藏的概念密切相关,维基百科定义如下:

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

在计算机科学中,信息隐藏是将计算机程序中最有可能发生变化的设计决策隔离的原则,从而在设计决策发生变化时保护程序的其他部分免遭大量修改.保护涉及提供一个稳定的接口,以保护程序的其余部分不受实现(最有可能更改的细节)的影响.

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

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:

封装是将数据和功能打包成单个组件.大多数面向对象的编程语言中的类都支持封装的特性,尽管也存在其他替代方案.它通过构建一堵无法穿透的墙来保护代码免受意外损坏,从而允许有选择地隐藏对象中的属性和方法.

也就是说,封装是一种信息隐藏(我们隐藏字段或方法).

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(above) 说,一个新的、更好的实现可以取代旧的.".这是抽象的目标,而不是封装的目标.我说得对吗?

由于封装可用于强制抽象,因此它可以促进它们的使用,因此有助于收获它们的好处.其实这就是封装的目的.

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.

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

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

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.

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

06-22 07:10