1. 程式人生 > 其它 >java 學習筆記 AF&RI

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() { ... } }
生命中真正重要的不是你遭遇了什麼,而是你記住了哪些事,又是如何銘記的。