什么是java中的类不变量?

[英]What is a class invariant in java?


I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles.

我搜索了这个主题,但除了维基百科,我没有找到任何进一步有用的文档或文章。

Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation?

任何人都可以用简单的语言向我解释它的含义或者向我推荐一些简单易懂的文档吗?

3 个解决方案

#1


67  

It doesn't mean anything in particular in reference to java.

它并不意味着特别指向java。

A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.

类不变量只是一个属性,它始终保存类的所有实例,无论其他代码是什么。

For example,

例如,

class X {
  final Y y = new Y();
}

X has the class invariant that there is a y property and it is never null and it has a value of type Y.

X具有类不变量,即存在y属性,它永远不为null,并且其值为Y.

class Counter {
  private int x;

  public int count() { return x++; }
}

fails to maintain two important invariants

无法保持两个重要的不变量

  1. That count never returns a negative value because of possible underflow.
  2. 由于可能的下溢,该计数永远不会返回负值。
  3. That calls to count are strictly monotonically increasing.
  4. 对计数的要求严格单调增加。

The modified class preserves those two invariants.

修改后的类保留了这两个不变量。

class Counter {
  private int x;

  public synchronized int count() {
    if (x == Integer.MAX_VALUE) { throw new IllegalStateException(); }
    return x++;
  }
}

but fails to preserve the invariant that calls to count always succeed normally (absent TCB-violations) because count might throw an exception or it might block if a deadlocked thread owns the counter's monitor.

但是无法保留计数调用总是正常成功(不存在TCB违规†)的不变量,因为count可能会抛出异常,或者如果死锁线程拥有计数器的监视器,它可能会阻塞。

Each language with classes make it easy to maintain some class invariants but not others. Java is no exception:

每个带有类的语言都可以很容易地维护一些类不变量而不是其他类。 Java也不例外:

  1. Java classes consistently have or do not have properties and methods, so interface invariants are easy to maintain.
  2. Java类始终具有或不具有属性和方法,因此界面不变量易于维护。
  3. Java classes can protect their private fields, so invariants that rely on private data are easy to maintain.
  4. Java类可以保护其私有字段,因此易于维护依赖于私有数据的不变量。
  5. Java classes can be final, so invariants that rely on there being no code that violates an invariant by crafting a malicious subclass can be maintained.
  6. Java类可以是最终的,因此可以维护依赖于通过制作恶意子类而不存在违反不变量的代码的不变量。
  7. Java allows null values to sneak in in many ways, so it is tough to maintain "has a real value" invariants.
  8. Java允许空值以多种方式潜入,因此很难保持“具有实际值”的不变量。
  9. Java has threads which means that classes that do not synchronize have trouble maintaining invariants that rely on sequential operations in a thread happening together.
  10. Java具有线程,这意味着不同步的类在维护依赖于一起发生的线程中的顺序操作的不变量时会遇到困难。
  11. Java has exceptions which makes it easy to maintain invariants like "returns a result with property p or returns no result" but harder to maintain invariants like "always returns a result".
  12. Java有一些例外,可以很容易地维护不变量,例如“返回带有属性p的结果或不返回结果”,但更难维护“始终返回结果”等不变量。

† - An externality or TCB violation is an event which a systems designer optimistically assumes will not happen.

† - 外部性或TCB违规是系统设计者乐观地认为不会发生的事件。

Typically we just trust that the basic hardware works as advertised when talking about properties of high-level languages built on them, and our arguments that invariants hold don't take into account the possibility of:

通常我们只相信基本硬件在讨论构建在它们上面的高级语言的属性时就像宣传的那样工作,并且不变量持有的参数没有考虑到以下可能性:

  • A programmer using debug hooks to alter local variables as a program runs in ways that code cannot.
  • 程序员使用调试钩子来改变局部变量,因为程序以代码不能运行的方式运行。
  • Your peers don't use reflection with setAccessible to modify private lookup tables.
  • 您的对等方不使用setAccessible的反射来修改私有查找表。
  • Loki altering physics causing your processor to incorrectly compare two numbers.
  • Loki改变物理特性导致处理器错误地比较两个数字。

For some systems our TCB might include only parts of the system, so we might not assume that

对于某些系统,我们的TCB可能只包含系统的一部分,因此我们可能不会这样认为

  • An administrator or privileged daemon won't kill our JVM process,
  • 管理员或特权守护程序不会终止我们的JVM进程,

but we might assume that

但我们可能会认为

  • We can checkpoint to a reliable transactional file-system.
  • 我们可以检查点到可靠的事务文件系统。

The higher-level a system, the larger its TCB typically is, but the more unreliable things you can get out of your TCB, the more likely your invariants are to hold, and the more reliable your system will be in the long run.

系统级别越高,其TCB通常越大,但是从TCB中获得的事物越不可靠,您的不变量就越有可能保持不变,从长远来看系统的可靠性就越高。

#2


9  

They are facts that must be true about an instance class. For example if a class has a property X and invariant can be X must be greater then 0. To my knowledge there is no built-in method for maintaining invariants you must make properties private and make sure your getters and setters enforce invariance property.

它们是关于实例类必须是真实的事实。例如,如果一个类具有属性X且不变,则X必须大于0.据我所知,没有用于维护不变量的内置方法,必须使属性为私有,并确保getter和setter强制执行不变属性。

There are annotations available which can check properties using reflection and interceptors. http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html

有一些注释可以使用反射和拦截器检查属性。 http://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html

#3


8  

Invariant means something that should stick to its conditions no matter whatever changes or whoever uses/transforms it. That is to say, a property of a class always fulfills or satisfies some condition even after going through transformations by using public methods. So, the client or user of this class is ensured about the class and its property.

不变意味着一些东西应该坚持其条件,无论任何变化或任何使用/转换它的人。也就是说,即使在通过使用公共方法进行转换之后,类的属性总是满足或满足某些条件。因此,确保该类及其属性的此类的客户端或用户。

For example,

例如,

  1. condition on function argument is that, it should always be > 0 (greater than zero) or should not be null.
  2. 函数参数的条件是,它应始终> 0(大于零)或不应为null。
  3. minimum_account_balance property of an account class states, it cannot go below 100. So all public functions should respect this condition and ensure class invariant.
  4. account class的minimum_account_balance属性表示,它不能低于100.因此所有公共函数都应该遵守这个条件并确保类不变。
  5. rule based dependency between variables, that is, value of one variable depends on another, so if one changes, using some fix-rule, other must also change. This relationship between 2 variables must be preserved. If it does not, then invariant is violated.
  6. 基于规则的变量之间的依赖关系,即一个变量的值取决于另一个,所以如果一个变化,使用一些fix-rule,其他变量也必须改变。必须保留2个变量之间的这种关系。如果没有,则违反不变量。
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/01/17/24b91011e855da44e64f03cfb567b77c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告