zoukankan      html  css  js  c++  java
  • Effective Java 19 Use interfaces only to define types

    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.

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    使用C实现一个函数内两个阿拉伯数字的交换
    使用C比较两个字符串是否相等
    C打印99乘法表
    C定时在控制台输出时分秒
    C实现猜数字
    OpenCV实现开操作、闭操作、形态学梯度、顶帽、黑帽
    OpenCV实现膨胀和腐蚀
    metaclass of python
    Django设计哲学
    using NGINX+uWSGI to deploy django project
  • 原文地址:https://www.cnblogs.com/haokaibo/p/use-interfaces-only-to-define-types.html
Copyright © 2011-2022 走看看