`
leonzhx
  • 浏览: 793651 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 16: Favor composition over inheritance

阅读更多

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".zip

    AdapterDelegates, 用于RecyclerView适配器的"Favor composition over inheritance" AdapterDelegates阅读这个项目的动机在我的博客文章。依赖项这里库在 Maven 中心可用:compile '...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    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 ...

    Android代码-AdapterDelegates-ListView

    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 ...

    AdapterDelegates:RecyclerView Adapters的“继承中的重要组成”

    Favor composition over inheritance 想法是为每种视图类型定义一个AdapterDelegate。 该委托负责创建ViewHolder并为特定视图类型绑定ViewHolder。 然后,您可以通过注册您真正需要的AdapterDelegates来组成...

    Android代码-Android 上漂亮的 Dialog 效果

    > 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 ...

    OO设计原则总结.doc

    #### 组合优于继承:Favor Composition Over Inheritance 尽管继承在某些场景下非常有用,但它也可能带来不必要的耦合和复杂性。组合原则提倡通过对象组合而非继承来实现代码复用。组合的优点包括: - **减少耦合*...

    java开发面向对象原则

    二、优先使用组合而非继承(Favor Composition Over Inheritance) 继承是一种常见的编程方式,但是它也有其缺点。继承会使得子类与父类紧耦合,难以维护和修改。 组合是指将多个对象组合成一个新的对象,这种方式...

    AVEVA PML快速入门引导

    这倒也符合 GoF 的《Design Patterns-Elements of Reusable of Object-Oriented Software》书中的一个建议:Favor object composition over class inheritance,即优先使用对象组合而不是继承。 PML 中的对象...

    C# 面向对象的基本原则

    C#面向对象的基本原则 一、面向接口编成而不是实现 [Code to an interface rather than to an implementation.] 二、优先使用组合而非继承 [Favor Composition Over Inheritance.] 三、SRP: The single ...

    java面向对象设计原则(带书签高清晰pdf)

    本文档将重点介绍两个核心原则:**优先使用对象组合而非类继承**(Favor Composition Over Inheritance, FCOI)。 #### 二、面向对象设计原则之FCOI详解 ##### 1. 对象组合(Composition) **定义:** 对象组合是...

    Qt class designing rule(Qt类设计规范)

    优先使用组合而非继承 (Favor Composition Over Inheritance) 在设计类结构时,应优先考虑使用组合(has-a)关系,而不是继承(is-a)。继承可能导致类层次过于复杂,而组合则更加灵活,可以更自由地组合对象的功能...

    OO设计原则总结

    - **优先使用组合而非继承(Favor Composition Over Inheritance)**:在多数情况下,使用组合而非继承来扩展类的功能可以减少代码复杂性和提高系统的可维护性。 #### 单一职责原则(SRP) **定义**: 单一职责...

    面向对象基本原则 面向对象基本原则

    #### 组合优先原则(Favor Composition Over Inheritance) 组合优先原则建议:“优先使用对象组合,而非类继承。”这一原则认为,相比于继承,组合提供了更灵活的方式来构建类之间的关系。组合允许一个类包含另一个...

    最新中兴和华为的最全笔试题(含09年)

    - 继承和组合的使用场景选择,尽量减少浅继承,提倡"favor object composition over class inheritance"。 - 使用const和引用参数,提高程序安全性。 - 设计模式:单例、工厂、观察者等设计模式在面试中也常被...

    35编程范式游记(6)- 面向对象编程1

    在面向对象编程中,有两种重要的设计原则:Program to an interface, not an implementation和Favor object composition over class inheritance。前者强调使用者不需要知道数据类型、结构、算法的细节,只需要知道...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...

Global site tag (gtag.js) - Google Analytics