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

Item 18: Prefer interfaces to abstract classes

阅读更多

1.  Existing classes can be easily retrofitted to implement a new interface.

 

2.  Interfaces are ideal for defining mixins.

 

3.  A mixin is a type that a class can implement in addition to its “primary type” to declare that it provides some optional behavior. For example, Comparable is a mixin interface that allows a class to declare that its instances are ordered with respect to other mutually comparable objects.

 

4.  Interfaces allow the construction of nonhierarchical type frameworks.

 

5.  Using interfaces to define types does not prevent you from providing implementation assistance to programmers. You can combine the virtues of interfaces and abstract classes by providing an abstract skeletal implementation class to go with each nontrivial interface that you export. (i.e. AbstractCollection, AbstractSet, AbastractMap, AbstractList, etc.)

 

6.  For most implementors of an interface, extending the skeletal implementation is the obvious choice. Furthermore, the skeletal implementation can still aid the implementor’s task if the class implementing the interface is implemented manually. It can forward invocations of interface methods to a contained instance of a private inner class that extends the skeletal implementation. This technique, known as simulated multiple inheritance, is closely related to the wrapper class idiom discussed in Item 16. It provides most of the benefits of multiple inheritance, while avoiding the pitfalls.

 

7.  Writing a skeletal implementation is relatively simple. First you must study the interface and decide which methods are the primitives in terms of which the others can be implemented. These primitives will be the abstract methods in your skeletal implementation. Then you must provide concrete implementations of all the other methods in the interface.

 

8.  A simple implementation is like a skeletal implementation in that it implements an interface and is designed for inheritance, but it differs in that it isn’t abstract: it is the simplest possible working implementation.(i.e. AbstractMap.SimpleEntry)

 

9.  It is far easier to evolve an abstract class than an interface. Once an interface is released and widely implemented, it is almost impossible to change.

 

10.  An interface is generally the best way to define a type that permits multiple implementations. An exception to this rule is the case where ease of evolution is deemed more important than flexibility and power. If you export a nontrivial interface, you should strongly consider providing a skeletal implementation to go with it. Finally, you should design all of your public interfaces with the utmost care and test them thoroughly by writing multiple implementations.

分享到:
评论

相关推荐

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

    Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity Item 22: Use interfaces only to define types Item 23: Prefer class hierarchies to tagged classes Item 24: Favor ...

    专享:prefer的用法__(全).pdf

    18. 未完的句子“18.I would prefer…”暗示说话者有某种希望或偏好,但此处句子没有给出完整的后半部分。 通过以上分析和例句,我们可以看出`prefer`这个词在不同情境中的使用方式,以及如何根据语境选择合适的...

    Effective C#

    **Item 18: Distinguish Between Value Types and Reference Types** - **Difference:** Value types store their data directly, while reference types store references to data stored elsewhere. - **...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    新人教九年级英语unit单词讲解PPT课件.pptx

    4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...

    effective c++

    2. **Item 7: Prefer Initialization to Assignment** - **核心观点**:提倡初始化而非赋值,尤其是在构造函数和成员变量的初始化过程中。 - **应用场景**:适用于对象创建时的资源管理,有助于减少内存泄漏和提高...

    介词to的用法归纳.pdf

    * prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...

    安徽省太和县北城中心学校九年级英语全册Unit9IlikemusicthatIcandancetoSectionA1a_2d导学

    3. prefer sth to sth,如:I prefer fish to meat. 或 I prefer reading to playing. 通过以上内容的学习,学生应能理解并运用这些词汇和语法结构,进行关于音乐喜好的交流,同时也能理解和使用定语从句,提升英语...

    九年级英语下册 Unit 6 Topic1 ABCD学案(无答案) 仁爱版

    - 宁愿…也不…:prefer…to… - 儿童节目:children's program - 新闻报道:news report - 体育节目:sports program - 教育节目:educational program - 娱乐节目:entertainment program - 爱上…:fall ...

    高考英语重点词组固定搭配大全.docx

    18. plan to do sth.:计划做某事,如:We're planning to visit Europe next year. 19. prepare to do sth.:准备做某事,如:Get ready to start your presentation. 20. pretend to do sth.:假装做某事,如:He ...

    ESLint 规则文件

    ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。

    初中要用到的固定搭配.docx

    - prefer to do sth / preferring doing sth.: 更喜欢做某事,如:I prefer to walk / I prefer walking. - begin to do sth / beginning doing sth.: 开始做某事,如:She began to cook / She began cooking. -...

    schemalint:Lint数据库架构

    public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...

    2013年九年级英语下册 Module 8 On the town动词不定式专项练习 外研版

    解析:"prefer to do... rather than do..." 表示宁愿做...而不愿做...,所以两个位置都应使用动词不定式。 4. — What do you think of tomorrow’s football match? — It’s difficult for us to win the match....

    英语词汇固定搭配.doc

    18. plan to do sth. - 计划做某事,例如:We're planning to go camping next weekend. 19. prepare to do sth. - 准备做某事,例如:She's preparing to give a speech at the conference. 20. pretend to do sth....

    九年级英语上册重点短语默写卷精选.doc

    8. 更愿意做…而不愿意做…:prefer to do…rather than do… 9. 从现在起:from now on 10. 使某人做某事:make sb. do sth. 11. 几个:several 12. 打开、关闭:open / close 13. 由…制成(看得见原料):be made ...

    2013年中考英语 单选题专项练习(第1集)

    7. 题目考察固定搭配:prefer to do sth. rather than do sth. 意为“宁愿做…而不愿做…”,所以B. to stay 是正确答案。 8. 题目考察动词短语的搭配:borrow 意为“借入”,lend 意为“借出”,所以A. borrow, ...

    山东省龙口市诸由观镇诸由中学九年级英语全册《Unit 4 I like music that I can dance to》练习

    - 更喜欢做:prefer to do - 他最著名的一首曲子:his most famous piece - 中国民族音乐:Chinese folk music - 感觉到了强烈的伤痛:feel a strong pain - 如此… 以至于:so...that... - 查阅;查询:look ...

    2015秋九年级英语上册Unit5ArtworldWelcometotheunit导学案无答案新版牛津版

    - 用法包括:prefer A to B(比起B更喜欢A),prefer doing A to doing B,以及prefer that sb. (should) do sth.。 - 造句示例:I prefer reading books to watching TV. 6. **课堂活动**: - 【演练展示】部分...

Global site tag (gtag.js) - Google Analytics