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() { ... }
}

 

相关文章:

  • 2021-12-05
  • 2021-06-05
  • 2021-04-22
  • 2021-12-29
猜你喜欢
  • 2022-01-11
  • 2022-01-11
  • 2021-09-20
  • 2022-01-31
  • 2021-06-24
  • 2022-12-23
  • 2021-05-17
相关资源
相似解决方案