java中用于共享“常量"的静态字段接口 [英] Interfaces with static fields in java for sharing 'constants'

查看:26
本文介绍了java中用于共享“常量"的静态字段接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些开源 Java 项目以了解 Java,并注意到其中很多都有某种常量"接口.

I'm looking at some open source Java projects to get into Java and notice a lot of them have some sort of 'constants' interface.

例如,processing.org 有一个名为 PConstants.java 和大多数其他核心类实现这个接口.接口充满了静态成员.这种方法是否有原因,或者这被认为是不好的做法?为什么不使用枚举在有意义的地方,或者使用静态类?

For instance, processing.org has an interface called PConstants.java, and most other core classes implement this interface. The interface is riddled with static members. Is there a reason for this approach, or is this considered bad practice? Why not use enums where it makes sense, or a static class?

我觉得使用接口来允许某种伪全局变量"很奇怪.

I find it strange to use an interface to allow for some sort of pseudo 'global variables'.

public interface PConstants {

  // LOTS OF static fields...

  static public final int SHINE = 31;

  // emissive (by default kept black)
  static public final int ER = 32;
  static public final int EG = 33;
  static public final int EB = 34;

  // has this vertex been lit yet
  static public final int BEEN_LIT = 35;

  static public final int VERTEX_FIELD_COUNT = 36;


  // renderers known to processing.core

  static final String P2D    = "processing.core.PGraphics2D";
  static final String P3D    = "processing.core.PGraphics3D";
  static final String JAVA2D = "processing.core.PGraphicsJava2D";
  static final String OPENGL = "processing.opengl.PGraphicsOpenGL";
  static final String PDF    = "processing.pdf.PGraphicsPDF";
  static final String DXF    = "processing.dxf.RawDXF";


  // platform IDs for PApplet.platform

  static final int OTHER   = 0;
  static final int WINDOWS = 1;
  static final int MACOSX  = 2;
  static final int LINUX   = 3;

  static final String[] platformNames = {
    "other", "windows", "macosx", "linux"
  };

  // and on and on

}

推荐答案

这通常被认为是不好的做法.问题是常量是实现类的公共接口"(为了更好的词)的一部分.这意味着实现类将所有这些值发布到外部类,即使它们只在内部需要.常量在整个代码中激增.一个例子是 Swing 中的 SwingConstants 接口,它由数十个类实现,这些类都重新导出"所有其常量(即使是它们不使用的常量)作为自己的常量.

It's generally considered bad practice. The problem is that the constants are part of the public "interface" (for want of a better word) of the implementing class. This means that the implementing class is publishing all of these values to external classes even when they are only required internally. The constants proliferate throughout the code. An example is the SwingConstants interface in Swing, which is implemented by dozens of classes that all "re-export" all of its constants (even the ones that they don't use) as their own.

但不要只相信我的话,JoshBloch 还说这很糟糕:

But don't just take my word for it, Josh Bloch also says it's bad:

常量接口模式是接口使用不当.一个类在内部使用一些常量是一个实现细节.实现一个常量接口会导致这个实现细节泄漏到类的导出 API 中.类实现常量接口对类的用户没有任何影响.事实上,它甚至可能使他们感到困惑.更糟糕的是,它代表了一种承诺:如果在未来的版本中修改了类,使其不再需要使用常量,它仍然必须实现接口以确保二进制兼容性.如果一个非final类实现了一个常量接口,那么它的所有子类的命名空间都会被接口中的常量污染.

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 is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, 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. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

枚举可能是更好的方法.或者您可以简单地将常量作为公共静态字段放在无法实例化的类中.这允许另一个类访问它们而不会污染它自己的 API.

An enum may be a better approach. Or you could simply put the constants as public static fields in a class that cannot be instantiated. This allows another class to access them without polluting its own API.

这篇关于java中用于共享“常量"的静态字段接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆