Java可扩展常量
我有一个软件设置,有两层,一个核心层和一个特定于客户的层。核心层定义了客户特定层应该能够扩展的常量。更具体地说:Java可扩展常量,java,inheritance,constants,Java,Inheritance,Constants,我有一个软件设置,有两层,一个核心层和一个特定于客户的层。核心层定义了客户特定层应该能够扩展的常量。更具体地说: public class CoreConstants { public static long CORE_CONSTANT_1 = 1; public static long CORE_CONSTANT_2 = 2; } 特定于客户的层应该能够添加仅在特定于客户的层中使用的常量。想法: public class CustomerConstants extends Core
public class CoreConstants
{
public static long CORE_CONSTANT_1 = 1;
public static long CORE_CONSTANT_2 = 2;
}
特定于客户的层应该能够添加仅在特定于客户的层中使用的常量。想法:
public class CustomerConstants extends CoreConstants
{
public static long CUSTOMER_CONSTANT_1 = 1000; // starting range = 1000
}
有没有更常见的方法来处理这个问题
更多信息:继承的原因是定义客户特定常量的起始范围。在CoreContents类中,我可以设置特定于客户的常量的起始值。然后,可以定义特定于客户的常量,如下所示:
public static long CUSTOMER_CONSTANT_1 = customStartValue + 1;
public static long CUSTOMER_CONSTANT_2 = customStartValue + 2;
没有理由在这两个类之间有继承机制。继承用于多态性,这里只有静态成员。只要有两个独立的课程。我甚至会让它们成为最终的、不可实例化的:
public final class CoreConstants {
/**
* Private constructor to prevent unnecessary instantiations
*/
private CoreConstants() {
}
}
没有理由在这两个类之间有继承机制。继承用于多态性,这里只有静态成员。只要有两个独立的课程。我甚至会让它们成为最终的、不可实例化的:
public final class CoreConstants {
/**
* Private constructor to prevent unnecessary instantiations
*/
private CoreConstants() {
}
}
整数常量通常最好替换为
enum
s,您可以使用enum上的接口实现所需的功能
interface CoreConstant {
int intValue();
}
enum CoreConstants implements CoreConstant {
CORE_CONSTANT_1(1),
CORE_CONSTANT_2(2);
private final int intValue;
public CoreConstants(int intValue) { this.intValue = intValue; }
public int intValue() { return intValue; }
}
interface CustomerConstant extends CoreConstant {}
enum CustomerConstants implements CustomerConstant {
CUSTOMER_CONSTANT_1(1000);
private final int intValue;
public CustomerConstants(int intValue) { this.intValue = intValue; }
public int intValue() { return intValue; }
}
您也许可以通过在枚举中使用委托,使用IntConstant
类来改进设计。不幸的是,您无法扩展枚举。结果是enum类中有一点代码重复
否则,如果希望继续使用公共静态int模型,则使用接口而不是类,并最终确定常量
interface CoreConstants {
public static final int CORE_CONSTANT_1 = 1;
}
整数常量通常最好替换为
enum
s,您可以使用enum上的接口实现所需的功能
interface CoreConstant {
int intValue();
}
enum CoreConstants implements CoreConstant {
CORE_CONSTANT_1(1),
CORE_CONSTANT_2(2);
private final int intValue;
public CoreConstants(int intValue) { this.intValue = intValue; }
public int intValue() { return intValue; }
}
interface CustomerConstant extends CoreConstant {}
enum CustomerConstants implements CustomerConstant {
CUSTOMER_CONSTANT_1(1000);
private final int intValue;
public CustomerConstants(int intValue) { this.intValue = intValue; }
public int intValue() { return intValue; }
}
您也许可以通过在枚举中使用委托,使用IntConstant
类来改进设计。不幸的是,您无法扩展枚举。结果是enum类中有一点代码重复
否则,如果希望继续使用公共静态int模型,则使用接口而不是类,并最终确定常量
interface CoreConstants {
public static final int CORE_CONSTANT_1 = 1;
}
谢谢你的列举,可能对我有用。关于使用接口,为什么这比使用类要好呢?如果“类型”仅仅是常量的容器,那么您永远不能实例化它。您可以通过私有化构造函数来实现这一点,但通常只使用接口更清晰。当然,除非您确实需要一些功能,例如
valueOf(String)
方法。在这种情况下,您将返回到一个类…@ptomli:用于保存常量的接口通常被视为反模式。看见在这种情况下,不需要公共静态最终修饰符,因为接口的所有字段都是隐式公共的、静态的和最终的。多谢枚举,在我的情况下可能会起作用。关于使用接口,为什么这比使用类要好呢?如果“类型”仅仅是常量的容器,那么您永远不能实例化它。您可以通过私有化构造函数来实现这一点,但通常只使用接口更清晰。当然,除非您确实需要一些功能,例如valueOf(String)
方法。在这种情况下,您将返回到一个类…@ptomli:用于保存常量的接口通常被视为反模式。看见在这种情况下,不需要公共静态最终修饰符,因为接口的所有字段都是隐式公共的、静态的和最终的。将此信息添加到原始问题中。我现在继承的原因是为特定于客户的常量定义起始值。将此信息添加到原始问题中。