gpt4 book ai didi

java - 如何为几个 Java 枚举添加常用方法? (抽象类的祖先?)

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:16 24 4
gpt4 key购买 nike

我有一些这样的 Java 枚举

public enum Aggregation
{
MORTGAGE( "Mortgage" ),
POOLS( "Pools" ),
PORTFOLIO( "Portfolio" );

private Aggregation( final String name )
{
m_Name = name;
}
private String m_Name;
static Map< String, Aggregation > c_LOOKUP =
new HashMap< String, Aggregation >();
static {
for (Aggregation agg:values()){
c_LOOKUP.put(agg.m_Name,agg);
}
}

public Aggregation lookup(String name){
return c_LOOKUP.get( name );
}

@Override
public String toString()
{
return m_Name;
}
}

public enum Interval
{
MONTHLY( "Monthly" ),
QUARTLY( "Quartly" ),
SEMIANNUALLY( "SemiAnnually" ),
ANNUALLY("Annually");

private Interval( final String name )
{
m_Name = name;
}
private String m_Name;
static Map< String, Interval > c_LOOKUP =
new HashMap< String, Interval >();
static {
for (Interval agg:values()){
c_LOOKUP.put(agg.m_Name,agg);
}
}

public Interval lookup(String name){
return c_LOOKUP.get( name );
}

@Override
public String toString()
{
return m_Name;
}
}

如您所见,这里有相当多的代码重复。如果有一种方法可以引入类似抽象公共(public)祖先类的东西,那就太好了。但是java enum不能固有。最好的方法是什么?谢谢。


编辑:我已经制定了一个类似于 ŁukaszBachman 和 missingfacktor 的版本

static public enum Aggregation
{
MORTGAGE( "Mortgage" ),
POOLS( "Pools" ),
PORTFOLIO( "Portfolio" );

private final String m_Name;

final static private ReverseDictionary< Aggregation > c_DICTIONARY =
new ReverseDictionary< Aggregation >( Aggregation.class );

static public Aggregation lookup( final String name )
{
return c_DICTIONARY.lookup( name );
}

private Aggregation( final String name )
{
m_Name = name;
}

@Override
public String toString()
{
return m_Name;
}
}

static public enum Interval
{
MONTHLY( "Monthly" ),
QUARTLY( "Quartly" ),
SEMIANNUALLY( "SemiAnnually" ),
ANNUALLY( "Annually" );

private final String m_Name;
final static private ReverseDictionary< Interval > c_DICTIONARY =
new ReverseDictionary< Interval >( Interval.class );

static public Interval lookup( final String name )
{
return c_DICTIONARY.lookup( name );
}

private Interval( final String name )
{
m_Name = name;
}

@Override
public String toString()
{
return m_Name;
}
}


static public class ReverseDictionary< E extends Enum< E >>
{
Map< String, E > c_LOOKUP = new HashMap< String, E >();

public ReverseDictionary( final Class< E > enumClass )
{
for( final E agg : EnumSet.allOf( enumClass ) )
{
c_LOOKUP.put( agg.toString(), agg );
}
}

public E lookup( final String name )
{
return c_LOOKUP.get( name );
}

}

我看到了一些推理。然而,它仍然不是很令人满意。

  1. lookup(String) 返回类型不同,很难定义接口(interface)
  2. 我可以理解 lookup(String) 并不是真正的重复,而是一种规范,但我仍然觉得 m_Name 字段和 toString() 逻辑有点多余。我们实际上是在指定一类枚举,在我看来它似乎是“is-a”关系。

最佳答案

倾向于组合而不是继承为了接口(interface)而编程。由于枚举是类(不是常规的,但仍然是类),您可以创建一些包含共享逻辑的字段,让枚举实现您的接口(interface)并将实现委托(delegate)给该字段。

相关代码片段:

共享接口(interface)

public interface MyInterface {

void someMethod();

}

逻辑实现

public class MyInterfaceImpl implements MyInterface {

public void someMethod() {
System.out.println("Do smth...");
}

}

第一个枚举

public enum EnumA implements MyInterface {
;

private MyInterface impl = new MyInterfaceImpl();

public void someMethod() {
impl.someMethod();
}

}

第二个枚举

public enum EnumB implements MyInterface {
;

private MyInterface impl = new MyInterfaceImpl();

public void someMethod() {
impl.someMethod();
}

}

请注意 EnumAEnumB 并不是真正的代码重复,因为那是简单的委托(delegate)(在我看来是有效的)。另请注意,所有内容都通过使用界面很好地粘合在一起。

关于java - 如何为几个 Java 枚举添加常用方法? (抽象类的祖先?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com