1. Inheriting from ordinary concrete classes across package boundaries is dangerous.
2. Unlike method invocation, inheritance violates encapsulation. The superclass’s implementation may change from release to release, and if it does, the subclass may break, even though its code has not been touched.
3. HashSet’s addAll method is implemented on top of its add method, although HashSet, quite reasonably, does not document this implementation detail.
4. A related cause of fragility in subclasses is that their superclass can acquire new methods in subsequent releases.
5. Instead of extending an existing class, give your new class a private field that references an instance of the existing class. This design is called composition because the existing class becomes a component of the new one. Each instance method in the new class invokes the corresponding method on the contained instance of the existing class and returns the results. This is known as forwarding, and the methods in the new class are known as forwarding methods.
6. A wrapper class is also known as the Decorator pattern.
7. Sometimes the combination of composition and forwarding is loosely referred to as delegation. Technically it’s not delegation unless the wrapper object passes itself to the wrapped object(so that wrapped object can pass it to other objects instead of passing the wrapped object itself.)
8. Wrapper classes are not suited for use in callback frameworks, wherein objects pass self–references to other objects for subsequent invocations (“callbacks”). Because a wrapped object doesn’t know of its wrapper, it passes a reference to itself (this) and callbacks elude the wrapper.
9. Inheritance is appropriate only if an “is-a” relationship exists between the two classes.
10. The wrapped class actually is the implementation detail of the wrapping class. If you use inheritance where composition is appropriate, you needlessly expose implementation details. The resulting API ties you to the original implementation, forever limiting the performance of your class. More seriously, by exposing the internals you let the client access them directly. At the very least, this can lead to confusing semantics.
11. If p refers to a Properties instance, then p.getProperty(key) may yield different results from p.get(key): the former method takes defaults into account, while the latter method, which is inherited from Hashtable, does not. In the case of Properties, the designers intended that only strings be allowed as keys and values, but direct access to the underlying Hashtable allows this invariant to be violated.
12. Inheritance propagates any flaws in the superclass’s API, while composition lets you design a new API that hides these flaws.
相关推荐
AdapterDelegates, 用于RecyclerView适配器的"Favor composition over inheritance" AdapterDelegates阅读这个项目的动机在我的博客文章。依赖项这里库在 Maven 中心可用:compile '...
Item 18: Favor composition over inheritance Item 19: Design and document for inheritance or else prohibit it Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity ...
Qt 类设计原则,内容不多,待续 – 面向接口编程而不是实现 (Code to an interface rather than to an implementation.) – 优先使用组合而非继承 (Favor Composition Over Inheritance)
Based on https://github.com/sockeqwe/AdapterDelegates." Favor composition over inheritance" for ListView Features Composable view handling on ListView Plugable data handler ViewHolder pattern for ...
Favor composition over inheritance 想法是为每种视图类型定义一个AdapterDelegate。 该委托负责创建ViewHolder并为特定视图类型绑定ViewHolder。 然后,您可以通过注册您真正需要的AdapterDelegates来组成...
> Favor composition over inheritance. LovelyDialog doesn't subclass any Dialog related classes, it is just a lightweight extensible wrapper for Dialog and manipulations with custom view. If you would ...
表演者 + 注意力机制的简单 Numpy 实现在Rethinking Attention with Performers ,以及在我的文章中复制实验的代码: 。
Favor A easy way of using Android SharedPreferences. How to use this library Using Gradle compile 'com.cocosw:favor:0.2.0@aar' Using Maven com.cocosw favor 0.2.0 apklib API 1 Define a ...
flutter_apprizee 一个新的Flutter应用程序。 入门 该项目是Flutter应用程序的起点。 如果这是您的第一个Flutter项目,那么有一些资源可以帮助您入门: 要获得Flutter入门方面的帮助,请查看我们的,其中提供了...
C#面向对象的基本原则 一、面向接口编成而不是实现 [Code to an interface rather than to an implementation.] 二、优先使用组合而非继承 [Favor Composition Over Inheritance.] 三、SRP: The single ...
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the(72页),资源名称:【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the...
java snmp 源码 ~ C/C++ CppCon2017会议资料 A curated list of awesome C/C++ performance optimization resources. c++ lib,大杂烩,什么都有 c++ http lib c++ lib 嵌入的高速数据同步库 ...内存型HAT-trie
Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...
domeafavor-android 爱彼迎 - Android
Por favor ingrese una dirección IP o 'salir' 1.2.3.4 Por favor ingrese una dirección IP o 'salir' 1.2.3.3 Por favor ingrese una dirección IP o 'salir' 1.2.3.1 Por favor ingrese una dirección IP o ...
POLAR品牌说明书
… has become a hot topic among people ...As far as I am concerned, my favor goes to the first/second view. Admittedly, …but it doesn’t follow that… Only if … can we…, just as the saying goes…
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the
Provide a data store with a graph abstraction (vertexes and edges), not keys/values Explicitly favor efficiency and availability over consistency
NULL 博文链接:https://z466459262.iteye.com/blog/901207