我有反射哈希码?
interface Module {
List<String> parse();
}
有接口的几种实现和应用程序,我想有一个HashSet<Module>
确保有每个模块只有一个实例。为此,我需要为每个班级提供适当的hashCode
。 hashCode
的正确实现将为一个模块类的不同实例返回相同的常量,但对于不同的模块将返回不同的实例。
要创建一个不错的设计方案,我想从像模块的名称计算的hashCode:
public int hashCode() {
return ConcreteModule.class.getName().hashCode();
}
但是这个代码将是接口的每一种实现......我的想法是相同的创建一个实现哈希码的抽象模块,但有可能达到扩展此抽象类的类的名称? 像:
public abstract class AbstractModule implements Module {
public int hashCode() {
// get the class name of class extending the abstract module
// and return hashcode of its string name
}
}
然后
public class ConcreteModule extends AbstractModule implements Module {
// implementation of parse() no need to create hashcode for each module
}
你会建议为每个模块创建哈希码或者是可以创造的东西我想?任何建议或意见,欢迎。在此先感谢
“每个模块只有一个实例”你是指每个实现类型?听起来你只需要一个'Dictionary'。 –
2013-05-03 17:43:32
这里不应该有一个单身人士符合法案吗? – CAFxX 2013-05-03 17:48:31
@JonSkeet是的,我的意思是最多可以有一个ConcreteModule1的实例,但也可以有一个ConcreteModule2的实例等等......您是否建议将它制作成像<“header”,HeaderModule>,< “Another”,AnotherModule>等等? – kuncajs 2013-05-03 17:48:48