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

Item 52: Refer to objects by their interfaces

阅读更多

1.  If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types. The only time you really need to refer to an object’s class is when you’re creating it with a constructor.

 

2.  If you get into the habit of using interfaces as types, your program will be much more flexible. If you decide that you want to switch implementations, all you have to do is change the class name in the constructor (or use a different static factory).

 

3.  There is one caveat: if the original implementation offered some special functionality not required by the general contract of the interface and the code depended on that functionality, then it is critical that the new implementation provide the same functionality. If you depend on any special properties of an implementation, document these requirements where you declare the variable.

 

4.  It is entirely appropriate to refer to an object by a class rather than an interface if no appropriate interface exists.(For example, value classes such as String and BigInteger.)

 

5.  A class that implements an interface but provides extra methods not found in the interface for example, LinkedHashMap should be used to refer to its instances only if the program relies on the extra methods.

 

分享到:
评论

相关推荐

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

    Item 64: Refer to objects by their interfaces Item 65: Prefer interfaces to reflection Item 66: Use native methods judiciously Item 67: Optimize judiciously Item 68: Adhere to generally accepted ...

    高中英语 知识点大全10 8refer torefer…to

    【高中英语知识点详解:refer to / refer…to】 在学习英语的过程中,掌握各种动词的用法至关重要。本文将详细解析“refer to”和“refer…to”这两个短语的含义和用法。 1. “refer to”通常作为不及物动词使用,...

    C++ Primer 源程序

    #include #include "Sales_item.h" int main() { Sales_item item1, item2; std::cin >> item1 >> item2;... std::cerr << "Data must refer to same ISBN" << std::endl; return -1; // indicate failure }

    CentOS6挂载linux7samba服务时报错:mount error(22): Invalid argument Refer to the mount.cifs(8) manual page (

    查看samba的版本是4版本的: [root@redhat_192.168.0.12 16:08:07 ~]#rpm -qa samba samba-4.9.1-6.el7.x86_64 找了好久,百度上一直都是mount命令加各种参数,确认了用户跟密码都没有问题,目录权限也都给了,最后...

    IDEA遇到Internal error. Please refer to http://jb. gg/ide/critical-startup-errors的问题及解决办法

    Please refer to http://jb.gg/ide/critical-startup-errors”是一个常见的启动错误提示。这个问题通常是由于IDEA在启动时加载了错误的配置或者第三方插件导致的,以下是一些解决这个问题的方法。 首先,当IDEA...

    MySql.Data.dll

    MySQL.Data.dll是MySQL数据库连接器的一个重要组件,用于在C#等.NET应用程序中与MySQL数据库进行交互。这个库由Oracle公司提供,它使得开发者能够利用C#的强大功能来设计、开发和部署基于MySQL的数据库应用。...

    referit-game-code-app:ReferIt游戏代码

    推荐游戏代码应用先决条件Ubuntu的:16.04 蒙戈:4.0.9 节点:10.16.0 npm:6.9.0安装运行npm install或通过npm install xxx --save package.json中的所有软件包准备NewestOrder.txt保存要收集的所有图像(目标对象...

    天马屏TM640480

    1.12 Outline Dimensions: Refer to outline drawing on next page 1.13 Dot Matrix: 640 X 480 Dots 1.14 Dot Size: 0.25X0.29 (mm) 1.15 Dot Pitch: 0.28X0.32 (mm) 1.16 Weight: Approx 1200g

    Android代码-openapi-generator

    :bangbang: To migrate from Swagger Codegen to OpenAPI Generator, please refer to the migration guide :bangbang: :notebook_with_decorative_cover: For more information, please refer to the Wiki page and...

    Embedded.Systems.Introduction

    In these books the terms LM3S LM4F and TM4C will refer to families of microcontrollers with the Texas Instruments Stellaris® line. Although the solutions are specific for the LM3S LM4F and TM4C ...

    jdk1.8-windows版64位

    5. **Default Methods in Interfaces:** This feature allows interfaces to have default implementations for their methods, which helps in adding new methods to existing interfaces without breaking ...

    CyUSB3.SYS和CyAPI.LIB的源代码

    This directory contains source code and build script for CyUSB3 WDF kernel mode USB generic... ->ReadMe.txt :Refer ReadMe.txt file for more information on streamer or bulkloop application. 4.ReadMe.txt

    T-REC-G.709.1-202005-I!Cor1!PDF-E.pdf

    - **Definition**: Flexible OTN short-reach interfaces refer to the optical interfaces designed for short distances (e.g., within data centers or between buildings). - **Features**: - **Flexibility**:...

    refer:引用表达式数据集API

    笔记该API能够加载所有4个参照表达数据集,即... " ReferItGame: Referring to Objects in Photographs of Natural Scenes. " EMNLP 2014.Yu, Licheng, et al. " Modeling Context in Referring Expressions. " E

    Semantic Co-segmentation in Videos

    In this paper, we propose to segment objects and understand their visual semantics from a collection of videos that link to each other, which we refer to as semantic co-segmentation. Without any ...

    RethinkDB的Clojure客户端revise.zip

    (require '[bitemyapp.revise.connection :refer [connect close]]) (require '[bitemyapp.revise.query :as r]) (require '[bitemyapp.revise.core :refer [run]]) ;; connect returns the connection agent (let ...

    Main.rar_Residential energy_d-optimal_demand control_smart home

    pricing refer to programs which offer incentives to customers who curtail their energy use during times of peak demand. In this paper, we propose an integrated solution to predict and re-engineer the ...

Global site tag (gtag.js) - Google Analytics