Designing an Abstract Type——Rules of thumb
设计简洁、一致的操作
简洁:操作要少而简单,不要多而复杂
一致:不应该在List中增加sum操作,如果客户端使用的是List, 会带来方便,但是如果使用List, 或nested lists嵌套列表,sum操作就会很难以理解和使用
要足以支持client对数据所做的所有操作需要,且用操作满足client需要的难度要低
足以支持所有操作需要:如List中要设置get方法,否则无法获取list内部数据
操作满足需要的难度要低:如使用List中的size(),比客户端中遍历获取size要方便
要么抽象、要么具体,不要混合 — 要么针对抽象设计,要么针对具体应用的设计
要么是泛型,要么是特定类型,不要又有泛型又有具体应用的设计
- A Deck type intended to represent a sequence of playing cards shouldn’t
have a generic add method that accepts arbitrary objects like integers or
strings. 牌组类型就是一个card序列,不应该拥有泛型的add方法 - Conversely, it wouldn’t make sense to put a domain-specific method like
dealCards into the generic type List .泛型类型中的操作不能有dealCards方法