3
关于 accept interfaces, return structs
source link: https://www.v2ex.com/t/816448
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
这句话出自 https://en.m.wikipedia.org/wiki/Robustness_principle
但是标准库也经常是返回 interface ,最简单就是 error,底层对上层屏蔽自己的实现细节,这不是很自然么
这句话我看很多文章都是在拿工厂模式说事,是不是只针对部分场景,如果上层只是想要一组通用的行为,底层返回 interface 没啥问题吧
4 条回复 • 2021-11-19 11:01:28 +08:00
2i2Re2PLMaDnghL 1 小时 11 分钟前
1. 我没发现你这个链接里有标题里这段话,如是来源具体历史版本,请提供;
2. Robustness principle 实质上导致了世纪初的 Web 生态大混乱:因为不是所有人都严格遵守之,而兼容性测试并未进行广泛配合;
3. 我认为最优兼容性实践是在所有未定义行为上人为施加随机性,依据是耗散结构理论。
2. Robustness principle 实质上导致了世纪初的 Web 生态大混乱:因为不是所有人都严格遵守之,而兼容性测试并未进行广泛配合;
3. 我认为最优兼容性实践是在所有未定义行为上人为施加随机性,依据是耗散结构理论。
Nitroethane 42 分钟前 via iPhone
最近在看 docker 源码,这种设计挺普遍的,方法的返回值类型是 interface ,实际 return 的是实现了 interface 的结构体实例。我觉着这种思路挺好啊,对外部屏蔽内部的实现细节
iyear 37 分钟前 via Android
@2i2Re2PLMaDnghL be conservative in what you do, be liberal in what you accept from others 算是这句话的口语化流传版本吧
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK