特征可以从基础实现抽象方法 class
Can a trait implement an abstract method from a base class
我有一个 ContactableMemberHandler
,即现在扩展了一些 RecordListHandler
,但我还需要一个扩展 SingleRecordHandler
的可联系成员处理程序版本。当然,该版本应该包含原始版本中的业务问题...
BaseRecordHandler
看起来像这样:
class BaseRecordHandler<T> {
// a lot of implementation details irrelevant to this question...
/**
*
* @param records
* @param onSubmit
* @param onCheckDuplicate
* @param onRetry
*/
public void handle(List<T> records, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry) {
boolean keepTrying = true;
while(keepTrying) {
try {
onSubmit();
keepTrying = false;
} catch (StepFailedException ex) {
this.handleFailure(ex, onSubmit, onCheckDuplicate, onRetry, records);
}
}
for (T n : records)
this.usedRecords.add(n);
}
protected abstract void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry, List<T> newRecords) throws StepFailedException;
// more irrelevant business logic
}
SingleRecordHandler
看起来像这样:
@InheritConstructors
public abstract class SingleRecordHandler<T> extends BaseRecordHandler<T> {
/**
* @param record
* @param onSubmit @throws StepFailedException
* @param onCheckDuplicate
* @param onRetry
*/
public void handle(T record, Closure onSubmit, Closure<Boolean> onCheckDuplicate, Closure<T> onRetry) {
this.handle([record], onSubmit, onCheckDuplicate, onRetry);
}
@Override
protected void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry,
List<T> newRecords) throws StepFailedException {
if (onCheckDuplicate(ex)) {
newRecords.add(onRetry());
}
else {
newRecords.pop();
throw ex;
}
}
}
和 RecordListHandler
看起来像这样:
@InheritConstructors
public abstract class RecordListHandler<T> extends BaseRecordHandler<T> {
@Override
protected void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry,
List<T> newRecords) throws StepFailedException {
int duplicateRecordIdx = onCheckDuplicate(ex);
if (duplicateRecordIdx > -1) {
newRecords.push(onRetry(duplicateRecordIdx));
} else {
newRecords.pop();
throw ex;
}
}
}
...但是如果我们使用特征呢?
听起来将 SingleRecordHandler
、RecordListHandler
转换为特征可以解决这个问题...
...但是当我们像这样使用它们时:
public class ContactableMemberListHandler extends BaseRecordHandler<ContactableMemberModel> implements RecordListHandler<ContactableMemberModel> {
// ... business logic
}
特征会handleFailure()
实现吗?
显然,确实如此。但是 trait
方法只有 private
和 public
,因此您必须使方法 public
.
一个trait
可以扩展一个class
,但是当你要使用特性时你仍然需要扩展那个基础class...
奇怪...
我有一个 ContactableMemberHandler
,即现在扩展了一些 RecordListHandler
,但我还需要一个扩展 SingleRecordHandler
的可联系成员处理程序版本。当然,该版本应该包含原始版本中的业务问题...
BaseRecordHandler
看起来像这样:
class BaseRecordHandler<T> {
// a lot of implementation details irrelevant to this question...
/**
*
* @param records
* @param onSubmit
* @param onCheckDuplicate
* @param onRetry
*/
public void handle(List<T> records, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry) {
boolean keepTrying = true;
while(keepTrying) {
try {
onSubmit();
keepTrying = false;
} catch (StepFailedException ex) {
this.handleFailure(ex, onSubmit, onCheckDuplicate, onRetry, records);
}
}
for (T n : records)
this.usedRecords.add(n);
}
protected abstract void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry, List<T> newRecords) throws StepFailedException;
// more irrelevant business logic
}
SingleRecordHandler
看起来像这样:
@InheritConstructors
public abstract class SingleRecordHandler<T> extends BaseRecordHandler<T> {
/**
* @param record
* @param onSubmit @throws StepFailedException
* @param onCheckDuplicate
* @param onRetry
*/
public void handle(T record, Closure onSubmit, Closure<Boolean> onCheckDuplicate, Closure<T> onRetry) {
this.handle([record], onSubmit, onCheckDuplicate, onRetry);
}
@Override
protected void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry,
List<T> newRecords) throws StepFailedException {
if (onCheckDuplicate(ex)) {
newRecords.add(onRetry());
}
else {
newRecords.pop();
throw ex;
}
}
}
和 RecordListHandler
看起来像这样:
@InheritConstructors
public abstract class RecordListHandler<T> extends BaseRecordHandler<T> {
@Override
protected void handleFailure(StepFailedException ex, Closure onSubmit, Closure onCheckDuplicate, Closure<T> onRetry,
List<T> newRecords) throws StepFailedException {
int duplicateRecordIdx = onCheckDuplicate(ex);
if (duplicateRecordIdx > -1) {
newRecords.push(onRetry(duplicateRecordIdx));
} else {
newRecords.pop();
throw ex;
}
}
}
...但是如果我们使用特征呢?
听起来将 SingleRecordHandler
、RecordListHandler
转换为特征可以解决这个问题...
...但是当我们像这样使用它们时:
public class ContactableMemberListHandler extends BaseRecordHandler<ContactableMemberModel> implements RecordListHandler<ContactableMemberModel> {
// ... business logic
}
特征会handleFailure()
实现吗?
显然,确实如此。但是 trait
方法只有 private
和 public
,因此您必须使方法 public
.
一个trait
可以扩展一个class
,但是当你要使用特性时你仍然需要扩展那个基础class...
奇怪...