Reason

The constant interface pattern is a poor use of interfaces.

That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API.

It represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility.

// Constant interface antipattern - do not use!

public interface PhysicalConstants {

// Avogadro's number (1/mol)

static final double AVOGADROS_NUMBER = 6.02214199e23;

// Boltzmann constant (J/K)

static final double BOLTZMANN_CONSTANT = 1.3806503e-23;

// Mass of the electron (kg)

static final double ELECTRON_MASS = 9.10938188e-31;

}

Solution

If the constants are strongly tied to an existing class or interface, you should add them to the class or interface. For example, all of the boxed numerical primitive classes, such as Integer and Double, export MIN_VALUE and MAX_VALUE constants. If the constants are best viewed as members of an enumerated type, you should export them with an enum type (Item 30). Otherwise, you should export the constants with a noninstantiable utility class (Item 4). Here is a utility class version of the PhysicalConstants example above:

// Constant utility class

package com.effectivejava.science;

public class PhysicalConstants {

private PhysicalConstants() { } // Prevents instantiation

public static final double AVOGADROS_NUMBER = 6.02214199e23;

public static final double BOLTZMANN_CONSTANT = 1.3806503e-23;

public static final double ELECTRON_MASS = 9.10938188e-31;

}

If you make heavy use of the constants exported by a utility class, you can avoid the need for qualifying the constants with the class name by making use of the static import facility, introduced in release 1.5:

// Use of static import to avoid qualifying constants

import static com.effectivejava.science.PhysicalConstants.*;

public class Test {

double atoms(double mols) {

return AVOGADROS_NUMBER * mols;

}

...

// Many more uses of PhysicalConstants justify static import

}

Summary

Interfaces should be used only to define types. They should not be used to export constants.

Effective Java 19 Use interfaces only to define types的更多相关文章

  1. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  2. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...

  3. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  4. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  5. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  6. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  7. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  8. Effective Java 第三版——19. 如果使用继承则设计,并文档说明,否则不该使用

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

随机推荐

  1. vertical-align两种应用场合

    vertical-align两种应用场合 (1)用在td/th中或display:table-cell元素中:让当前元素中的文本内容在竖直方向上居中    css部分:    .content{   ...

  2. sublime 插件zen coding

    sublime的插件Zen Coding是一个编写html的神器,现在已经更名为Emmet了. 在sublime中的package需要搜索的是Emmet 相关网站: 官网 Zen Coding: 一种 ...

  3. Excel中显示长数字的方法

    主要有以下三种方法: 1.先设置为文本格式,再粘贴2.在另一列输入=CONCATENATE(A1),双击此格右下角得到全部数值,再格式化为文本粘贴回去3.选中数据列,点数据-分列,下一步-下一步,选中 ...

  4. ASP.NET运行时详解 集成模式和经典模式

    遗留问题 在<ASP.NET运行时详解 生命周期入口分析>中遗留两个问题,包括Application的InitInternal方法执行细节.IIS6和II7经典模式请求管道管理类Appli ...

  5. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  6. 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式

    [源码下载] 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过 M ...

  7. mysql启动不起来了!

    [root@iZ28r2sl9qkZ data]# service mysqld restartMySQL server PID file could not be found! [FAILED]St ...

  8. 数据库的sacle-up和scale-out与sharding技术区分

    scale 英文是扩展的意思. 数据库要进行扩展,指的是存储系统不够,或者性能不够,要提升的时候,就要进行扩展. 分为向上扩展和横向扩展,这就像一个人往上面发展与横向发展两种思路. scale-up: ...

  9. 前端技巧:禁止浏览器static files缓存篇(转)

    前端技巧:禁止浏览器static files缓存篇 由于CSS/JS文件经常需要改动,前端调试时是不希望浏览器缓存这些文件的. 本文记录博主的经验. Meta法 目前在chrome调试还没有遇到问题, ...

  10. JVM中显示锁基础AbstractQueuedSynchronizer

    在研究AbstractQueuedSynchronizer的时候我是以ReentrantLock入手的.所以理所当然会设计到一些ReentrantLock的方法.因为网上已经有很多关于AQS的文章了, ...