HIT软件构造:3.Abstract Data Type (ADT)

目录

 

1.Abstraction and User-Defined Types

2.Classifying Types and Operations

3.Abstract Data Type Examples

4.Designing an Abstract Type

5.Representation Independence(表示独立性)


1.Abstraction and User-Defined Types

    A programming language came with built-in types (such as integers, booleans, strings, etc.) and built-in procedures, e.g., for input and output.

    An abstract data type Bool has the following operations:

– true: Bool 
– false: Bool
– and: Bool × Bool → Bool 
– or: Bool × Bool → Bool 
– not: Bool → Bool

一个ADT是由操作定义的,但是与他的内部实现无关。

2.Classifying Types and Operations

Mutable types(可变类型的对象):提供了可改变其内部数据的值的操作

Immutable types(不变数据类型): 其操作不改变内部值,而是构造新的对象

要注意的是,immutable中并不是绝对的不可以改变值,可以通过构造新的对象的方法对内部值进行修改。

creator : t* → T
producer : T+, t* → T
observer : T+, t* → t
mutator : T+, t* → void | t | T
Each T is the abstract type itself; 
Each t is some other type. 
The + marker indicates that the type may occur one or more times 
in that part of the signature.
The * marker indicates that it occurs zero or more times. 
The | indicates or.

3.Abstract Data Type Examples

int类型和String类型就是典型的immutable类型

int is immutable, so it has no mutators.
– creators: the numeric literals 0 , 1 , 2 , …
– producers: arithmetic operators + , - , * , /
– observers: comparison operators == , != , < , >
– mutators: none (it’s immutable)
String is Java’s string type. String is immutable.
– creators: String constructors
– producers: concat , substring , toUpperCase
– observers: length , charAt
– mutators: none

List是一个mutable类型

方法类型距离:

Integer.valueOf()                Creator
BigInteger.mod()                 Producer
List.addAll()                    Mutator
String.toUpperCase()             Producer
Set.contains()                   Observer
Map.keySet()                     Observer
Collections.unmodifiableList()   Producer
BufferedReader.readLine()        Mutator

4.Designing an Abstract Type

设计好的ADT,靠“经验法则”,提供一组操作,设计其行为规约spec

规则:

1.设计简洁、一致的操作

2.要足以支持client对数据所做的所有操作需要,且 用操作满足client需要的难度要低

5.Representation Independence(表示独立性)

    表示独立性:client使用ADT时无需考虑其内部如何实 现,ADT内部表示的变化不应影响外部spec和客户端。

    非ADT的操作指明了具体的pre-和post-condition,否则不能改变ADT的内部表示——spec规定了 client和implementer之间的契约。

    示例代码:

一种导致表示泄露的错误:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值