知识卡片
组合替代继承谱系
组合把较小类型作为较大类型的一部分,表达“拥有某种能力”,不要求先建立复杂的继承家谱。Go还按方法集合判断接口满足关系,使行为契约与具体类型声明解耦。这增加了组合与接口重用的灵活性;组合是否合适仍取决于实际职责,不等于把所有字段都匿名嵌入。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Why Go Rules the Cloud Native World · 查看原书依据
composition allows types to be constructed using “has a” relationships to define what they can do (i.e., a car “has a” motor). In practice, this permits greater design flexibility while allowing the creation of business domains that are less susceptible to disruption by the quirks of “family members”. By extension, while Go uses interfaces to
参考来源
- 位置:《Cloud Native Go》第02章《Why Go Rules the Cloud Native World》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch02.xhtml)
- 结论依据:组合把较小类型作为较大类型的一部分,表达“拥有某种能力”,不要求先建立复杂的继承家谱。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:composition allows types to be constructed using “has a” relationships to define what they can do (i.e., a car “has a” motor). In practice, this permits greater design flexibility while allowing the creation of business domains that are less susceptible to disruption by the quirks of “family members”. By extension, while Go uses interfaces to