java 学习笔记 AF&RI

Abstraction function——抽象函数
抽象函数是表示值到其对应的抽象值的映射——AF: R->A。
对于抽象函数来说,仅仅宽泛的说抽象域表示了什么并不够。抽象函数的作用是规定合法的表示值会如何被解释到抽象域。作为一个函数,我们应该清晰的知道从一个输入到一个输入是怎么对应的。
Rep invariant——表示不变量
注明抽象值的合法区域。
说明合法/不合法的原因。
代码示例

// Immutable type representing a tweet.
public class Tweet {

private final String author;
private final String text;
private final Date timestamp;

// Rep invariant:
// author is a Twitter username (a nonempty string of letters, digits, underscores)
// text.length <= 140
// Abstraction function:
// AF(author, text, timestamp) = a tweet posted by author, with content text, 
// at time timestamp 
// Safety from rep exposure:
// All fields are private;
// author and text are Strings, so are guaranteed immutable;
// timestamp is a mutable Date, so Tweet() constructor and getTimestamp() 
// make defensive copies to avoid sharing the rep's Date object with clients.

// Operations (specs and method bodies omitted to save space)
public Tweet(String author, String text, Date timestamp) { ... }
public String getAuthor() { ... }
public String getText() { ... }
public Date getTimestamp() { ... }
}

 

posted @ 2021-07-06 22:57  大奕哥&VANE  阅读(117)  评论(0编辑  收藏  举报