Is 'invariant' property part of the definition of Abstraction?

别说谁变了你拦得住时间么 提交于 2019-12-21 21:27:48

问题


As part of my learning i think the best answer(with meaning) for definition of abstraction that i found is from stackoverflow:

What is abstraction?

Besides that,

As part of current online course cs61B Fall 2006, Berkeley, i learnt the similar below definition of ADT close to above definition but added an extra word 'invariant'. Shall i consider this word as corollary to the above definition? or Is this word part of the definition?

An _Abstract_Data_Type_ (ADT) is a class that has a well-defined interface, but its implementation details are firmly hidden from other classes. That way, you can change the implementation of a class without jeopardizing the programs that depend on it. The Date class is an ADT.

ADT allows you to enforce invariants.

An invariant is a fact about a data structure that is always true (assuming the code is bug-free), no matter what methods are called by external classes. For example, the Date ADT enforces the invariant that a Date object always represents a valid date. An invariant is enforced by allowing access to certain fields only through method calls.


回答1:


One of my favourite definitions of abstraction is Robert C. Martin's, from the book Agile Principles, Patterns, and Practices:

Abstraction is the elimination of the irrelevant and the amplification of the essential.

Based on that definition, an abstraction can take many forms, on many different levels:

  • It can be an entire system
  • It can be a subsystem
  • It can be a definition of a message format (akin to a protocol)
  • It can be a function (as in Functional Programming)
  • It can be an abstract base class
  • It can be an interface
  • It can be a concrete class
  • etc.

In Object-Oriented Software Construction, Bertrand Meyer describes classes as Abstract Data Types. The idea in this book is that the description of an Abstract Data Type includes pre- and post-conditions. In Eiffel (the language used in the book), these are called assertions, but we also know them as invariants.

Meyer's view on Object-Oriented Design includes invariants. On a detailed (class/object/function) level, I agree that it makes sense to view invariants as a way to describe an abstraction.

On the other hand, if your abstraction is a description of a high-level architecture, those kind of invariants doesn't make sense. Still, even at a high level, it would often be valuable to define pre- and post-condition (e.g. "System A must be responding in order for System B to work correctly"), so it does makes sense to think about invariants in relation to abstractions.



来源:https://stackoverflow.com/questions/22186528/is-invariant-property-part-of-the-definition-of-abstraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!