简体   繁体   English

Java 在两个类几乎相似但包不同但功能相似时避免代码重复

[英]Java avoiding code duplication when both classes are almost similar but different packages, but similar functions

There is a literal code in my project,我的项目中有一个文字代码,

private List<String> getAddressLines(PartyAddressDTO partyAddressDTO) {
    List<String> addressLines = new ArrayList<>();
    if (!CollectionUtils.isEmpty(partyAddressDTO.getAddressLines()))
        addressLines.addAll(partyAddressDTO.getAddressLines().values());
    if (addressLines.isEmpty()) {
        if (!StringUtils.isEmpty(partyAddressDTO.getHouseNumber()))
            addressLines.add(partyAddressDTO.getHouseNumber());
        if (!StringUtils.isEmpty(partyAddressDTO.getStreet()))
            addressLines.add(partyAddressDTO.getStreet());
        if (!StringUtils.isEmpty(partyAddressDTO.getCity()))
            addressLines.add(partyAddressDTO.getCity());
    }
    return addressLines;
}

This is duplicated at three different places, I was trying to go through sonar analysis and this is the place it shows as duplicated, but the problem is,这在三个不同的地方重复,我试图通过声纳分析 go,这是它显示为重复的地方,但问题是,

com.package.module1.party.PartyAddressDTO
com.package.module2.party.PartyAddressDTO
com.package.module3.party.PartyAddressDTO

In our three different classes, this is the imports, all three belong to different packages, I don't understand why those source modules have been named like this, and I don't have control over their objects, and they don't extend a single interface or anything, but having to duplicate some lines of code in my project is en eyesore to me.在我们的三个不同的类中,这是导入,这三个属于不同的包,我不明白为什么那些源模块被这样命名,我无法控制它们的对象,它们也没有扩展一个单一的界面或任何东西,但不得不在我的项目中复制一些代码行对我来说是令人讨厌的。 Is there anyway to extract such code?反正有提取这样的代码吗?

If the properties/methods of PartyAddressDTO class is same for all 3 modules, and you dont have any control on those modules, then the best way to avoid code duplication is to have your own implementation like this: (please ignore the property names and method names)如果 PartyAddressDTO class 的属性/方法对于所有 3 个模块都相同,并且您对这些模块没有任何控制权,那么避免代码重复的最佳方法是拥有自己的实现,如下所示:(请忽略属性名称和方法名称)

public class MyPartyAddressDTO {
  private String name;
  private int age;

  public MyPartyAddressDTO(package1.PartyAddressDTO partyAddressDTO1){
    this.name = partyAddressDTO1.getName();
    this.age = partyAddressDTO1.getAge();
  }

  public MyPartyAddressDTO(package2.PartyAddressDTO partyAddressDTO2){
    this.name = partyAddressDTO2.getName();
    this.age = partyAddressDTO2.getAge();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
}

and use MyPartyAddressDTO throughout your application并在整个应用程序中使用 MyPartyAddressDTO

Generally, We would have a common module and have an internal pkg, extract the common parts to that place.通常,我们会有一个公共模块并有一个内部 pkg,将公共部分提取到那个地方。 All other modules would refer common but common would be referred by anyone, so cyclic dependency will be avoided.所有其他模块都将引用 common,但 common 将被任何人引用,因此将避免循环依赖。 Move all your common classes over there and you can avoid duplication.将所有常用类移到那里,可以避免重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM