[英]Where should I put the common code of the constructors?
我遇到的情況是,在一個類中我有2個構造函數,並且它們具有非常相似的代碼。 唯一的區別是對超類的構造函數的調用。 我應該把這個通用代碼放在哪里? 我嘗試使用實例塊,但是使用實例塊時,我無法傳遞所需的參數。
同樣,這些字段是最終的。 因此將無法初始化構造函數以外的其他函數。
我的代碼如下:
private final SourceCache sourceCache;
private final ServiceCache serviceCache;
private final MethodCache methodCache;
private final ModelCache modelCache;
private final QueryFactory queryFactory;
public MetaDataPersistenceHandler(
final Transaction transaction)
{
super(transaction);
this.transaction = transaction;
this.sourceCache = new SourceCache(transaction);
this.serviceCache = new ServiceCache(transaction);
this.methodCache = new MethodCache(transaction);
this.modelCache = new ModelCache(transaction);
this.queryFactory = new QueryFactory();
this.transaction.addQueryFactory(this.queryFactory);
}
public MetaDataPersistenceHandler(
final Transaction transaction,
final long fileSize)
{
super(transaction, fileSize);
this.transaction = transaction;
this.sourceCache = new SourceCache(transaction);
this.serviceCache = new ServiceCache(transaction);
this.methodCache = new MethodCache(transaction);
this.modelCache = new ModelCache(transaction);
this.queryFactory = new QueryFactory();
this.transaction.addQueryFactory(this.queryFactory);
}
如果您的實例字段是最終的,那么我將擁有不帶filesize
的構造函數,只需調用第二個並傳遞0
/ null
值即可。
另外,為什么要在子類中分配事務? 你不把它傳給父母嗎?
private final SourceCache sourceCache;
private final ServiceCache serviceCache;
private final MethodCache methodCache;
private final ModelCache modelCache;
private final QueryFactory queryFactory;
public MetaDataPersistenceHandler(
final Transaction transaction)
{
this(transaction, 0L); // Call the whole constructor below?
}
public MetaDataPersistenceHandler(
final Transaction transaction,
final long fileSize)
{
super(transaction, fileSize);
this.transaction = transaction; // Why are we setting this again?
this.sourceCache = new SourceCache(transaction);
this.serviceCache = new ServiceCache(transaction);
this.methodCache = new MethodCache(transaction);
this.modelCache = new ModelCache(transaction);
this.queryFactory = new QueryFactory();
this.transaction.addQueryFactory(this.queryFactory);
}
將其移至私有構造函數。
編輯:這是一個例子
public MetaDataPersistenceHandler(
final Transaction transaction) {
super(transaction);
this.transaction = transaction;
MetaDataPersistenceHandler();
}
public MetaDataPersistenceHandler(
final Transaction transaction,
final long fileSize) {
super(transaction, fileSize);
this.transaction = transaction;
MetaDataPersistenceHandler();
}
private void MetaDataPersistenceHandler(){
this.sourceCache = new SourceCache(transaction);
this.serviceCache = new ServiceCache(transaction);
this.methodCache = new MethodCache(transaction);
this.modelCache = new ModelCache(transaction);
this.queryFactory = new QueryFactory();
this.transaction.addQueryFactory(this.queryFactory);
}
更好的方法是使用Optional而不是0或null。
private final SourceCache sourceCache;
private final ServiceCache serviceCache;
private final MethodCache methodCache;
private final ModelCache modelCache;
private final QueryFactory queryFactory;
public MetaDataPersistenceHandler(
final Transaction transaction)
{
this(transaction, Optional.<Long>absent()); // Call the whole constructor below?
}
public MetaDataPersistenceHandler(
final Transaction transaction,
final Optional<Long> fileSize)
{
super(transaction, (fileSize.isPresent() ? fileSize.get() : 0));
this.transaction = transaction; // Why are we setting this again?
this.sourceCache = new SourceCache(transaction);
this.serviceCache = new ServiceCache(transaction);
this.methodCache = new MethodCache(transaction);
this.modelCache = new ModelCache(transaction);
this.queryFactory = new QueryFactory();
this.transaction.addQueryFactory(this.queryFactory);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.