【发布时间】:2013-09-17 05:34:04
【问题描述】:
我有一堆类似实用程序的方法,看起来非常相似,比如:
public static void addLeadingAttorney(EventAttorneyModel newAttorney,
List<EventAttorneyModel> existingAttorneys) {
for (EventAttorneyModel existingAttorney : existingAttorneys) {
existingAttorney.setSequence(existingAttorney.getSequence() + 1);
}
newAttorney.setSequence(1L);
existingAttorneys.add(0, newAttorney);
}
public static void addLeadingAttorney(CaseAttorneyModel newAttorney,
List<CaseAttorneyModel> existingAttorneys) {
for (CaseAttorneyModel existingAttorney : existingAttorneys) {
existingAttorney.setSequence(existingAttorney.getSequence() + 1);
}
newAttorney.setSequence(1L);
existingAttorneys.add(0, newAttorney);
}
EventAttorneyModel 和 CaseAttorneyModel 类是 JPA 实体,除了 Object 类之外没有共同的前身。
不知道有没有什么方法可以避免重复代码,因为以后会有很多这样的方法?
【问题讨论】:
-
它们没有共同的父级或接口是否有原因?
-
如果两个类都实现了相同的接口,这将很容易做到。
标签: java jpa refactoring code-duplication