0

经过SO问题后,我了解到,

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

抽象与分离接口实现有关。

从课堂java培训中,我了解到,封装具有以下优点,

为什么封装是你的朋友?

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

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

[3] ADT 可以保证它们的不变量被保留。

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

[5] 文档和可维护性。通过定义一个明确的接口,您可以让其他程序员更容易修复您离开公司多年后出现的错误。许多错误是模块之间不可预见的交互的结果。如果每个接口和每个模块的行为都有一个明确的规范,那么 bug 就更容易追踪。

[6] 当你的项目不工作时,更容易找出该怪哪个队友。

问题一:

Wrt Point1(上)说,“一个新的、更好的实现可以取代旧的。 ”。这是抽象而不是封装的目标。我对么?

问题2:

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

4

2 回答 2

0

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

Wikipedia defines abstraction as follows:

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.

and for mathematics:

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

and computer science:

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.

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:

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

于 2015-08-23T14:00:38.663 回答
0

(1)接口应广义解释;因为它具有一些特定的技术含义,所以我倾向于使用合同一词(来自“按合同设计”)来表示相同的意思。它是如何与特定模块(或某种模块)交互的正式规范。Collections API 就是一个很好的例子。aList通常是ArrayLista ,但它可以是 a LinkedList、 anImmutableList或其他更奇特的东西,例如延迟加载数据库代理。我几乎从不在乎我的代码是哪种特定类型;我只是与List界面交互。

(2) 如果使用实际的 Java 接口(或等价物),则很容易创建要在开发时使用的组件的模拟或脚手架版本。例如,我刚刚实现了一个发送电子邮件的系统。当我让系统的其余部分准备好实际发出要发送的命令时,我使用了一个虚假的邮件服务,它只是编写日志消息来假装发送邮件。这样,我可以测试我的系统中应该调用邮件服务的部分是否可以独立于实际发送邮件而正常工作。

在更大的范围内,我使用 Spring MVC 来处理我的应用程序的 Web 层。Spring MVC 与 Servlet API 交互以与 servlet 容器交互,该容器本质上是 Web 服务器本身。Spring 团队不需要了解有关服务器的任何其他信息。不同的团队已经编写了几个不同的 servlet 容器(Tomcat、Jetty、Undertow、WebLogic),但由于每个人都使用一个通用接口,因此他们不必进一步协调他们的开发工作。

于 2015-08-02T08:23:56.957 回答