← 返回刚才阅读的位置

知识卡片

结构图卡 · 5619.g.3

基本接口承载可复用集合算法

理解等级:中级

Set抽取Add、Contains、Len、Elements等基本能力,Same的参数改成Set后,*HashSet才按相同签名满足该接口。高级集合运算可作为接受Set的独立函数,复用不同实现的基本能力,而不要求每个实现重复编写。接口不是语法上禁止引用实现类型,而是此处选择降低耦合。

结构图:

classDiagram
class Set {
<<interface>>
Add(e interface{}) bool
Contains(e interface{}) bool
Len() int
Same(other Set) bool
Elements() []interface{}
}
class HashSet {
m map
Add(e interface{}) bool
Contains(e interface{}) bool
Len() int
Same(other Set) bool
Elements() []interface{}
}
class SetAlgorithms {
<<示意函数组>>
IsSuperset(one Set, other Set) bool
}
HashSet ..|> Set : 指针方法隐式满足
SetAlgorithms ..> Set : 依赖基本能力
这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Go并发编程实战》第04章《流程控制方法》之《4.4 实战演练——Set》(来源项目book-sources;源文件:extracted/Go并发编程实战/_epub-src/text/part0000_split_031.html) - 结论依据:原文围绕基本接口承载可复用集合算法给出定义、实例或限制;卡片结合这些上下文解释条件,已知错误在正文就近校正。 - 原始内容:type Set interface { Add(e interface{}) bool Remove(e interface{}) Clear() Contains(e interface{}) bool Len() int Same(other Set) bool Eleme