简体   繁体   中英

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,

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)

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

Generally, We would have a common module and have an internal pkg, extract the common parts to that place. All other modules would refer common but common would be referred by anyone, so cyclic dependency will be avoided. Move all your common classes over there and you can avoid duplication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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