【发布时间】:2014-06-08 08:20:06
【问题描述】:
简而言之,我正在尝试构建抽象类的子类,所有这些子类都将是单例。我只想将单例“逻辑”放在超类中。这在Java中可能吗?这是代码:
public abstract class Table {
//the static singleton instance. this will be inherited by subclasses of this class.
protected static Table m_Instance;
/**
* @param tableName the database table name.
*/
protected Table(String tableName, List<Column> columns) {
TABLE_NAME = tableName;
if(columns != null) {
if(!m_Columns.isEmpty())
m_Columns.clear();
m_Columns.addAll(columns);
} else {
throw new IllegalStateException("the columns list was null. this is a developer error. please report to support.");
}
}
protected static Table getInstance() {
if(m_Instance == null)
m_Instance = <? extends Table>;
}
}
这里只是一个用于澄清实施的简介:
public class CallTable extends Table {
//this class would inherit 'getInstance()' and the method would return a 'CallTable' object
}
【问题讨论】:
标签: java inheritance reflection singleton