A representation invariant is a condition concerning the state of an object. The condition can always be assumed to be true for a given object, and operations are required not to violate it.
In a Deck class, a representation invariant might be that there are always 52 Cards in the deck. A shuffle() operation is thus guaranteed not to drop any cards on the floor. Which in turn means that someone calling shuffle(), or indeed any other operation, does not need to check the number of cards before and after: they are guaranteed that it will always be 52.
发布评论
评论(1)
表示不变量是有关对象状态的条件。对于给定对象,条件始终可以假设为真,并且操作需要不违反它。
在 Deck 类中,表示不变量可能是牌组中始终有 52 张牌。因此,shuffle() 操作可以保证不会将任何牌掉落在地板上。这反过来意味着调用 shuffle() 或任何其他操作的人不需要检查之前和之后的卡片数量:他们保证它始终是 52。
A representation invariant is a condition concerning the state of an object. The condition can always be assumed to be true for a given object, and operations are required not to violate it.
In a Deck class, a representation invariant might be that there are always 52 Cards in the deck. A shuffle() operation is thus guaranteed not to drop any cards on the floor. Which in turn means that someone calling shuffle(), or indeed any other operation, does not need to check the number of cards before and after: they are guaranteed that it will always be 52.