方法

时间:2016-07-11 18:09:36

标签: java design-patterns coding-style checkstyle pmd

有时,我有一个关于我的代码的PMD检查问题。我的意思是 - 太多的字符串参数:

"不要使用大量的String参数,而是考虑为这些值使用容器对象"

f.e:

public void printPersons (final String name, final String surname, final String day, final String car, final String city, final String petName) 
{

}

如何在Yours项目中避免这种情况?

2 个答案:

答案 0 :(得分:0)

您可以定义一个封装所有这些字段的简单类,并将其传递给原始方法。

track by $index

现在你的方法调用变为:

public class Person {
  private final String name;
  private final String surname;
  private final String day;
  private final String car;
  private final String city;
  private final String petName;

  public Person(final String name, final String surname, final String day, final String car, final String city, final String petName) {
    this.name = name;
    this.surname = surname;
    this.day = day;
    this.car = car;
    this.city = city;
    this.petName = petName;
  }

  public String getName() {
    return this.name;
  }

  public String getSurname() {
    return this.surname;
  }

  public String getDay() {
    return this.day;
  }

  public String getCar() {
    return this.car;
  }

  public String getCity() {
    return this.city;
  }

  public String getPetName() {
    return this.petName;
  }
}

答案 1 :(得分:0)

For me it is out of question that a container object (Person) should be used here. To make the construction of Person instances easier a inner class PersonBuilder is used, no constructor or setters are exposed.

    public final class Person {
    private final String name;
    private final String surname;
    private final String day;
    private final String car;

    // all the other attributes omitted

    public static void main(final String[] args) {
        // usage example
        Person person = Person.builder().name("Name").surname("Surname")
                .car("Tesla").day("Friday").build();

        person.toString();

    }

    // private constructor, only used by the PersonBuilder
    private Person(final PersonBuilder pb) {
        this.name = pb.name;
        this.surname = pb.surname;
        this.day = pb.day;
        this.car = pb.car;
    }

    public static PersonBuilder builder() {
        return new PersonBuilder();
    }

    public String toString() {

        return "return a nicely formatted String for printing, logging etc.";
    }

    /**
     * expose a nice, fluent API to construct Persons.
     */
    public static final class PersonBuilder {
        private String name;
        private String surname;
        private String day;
        private String car;

        // use the static method Person.builder() to get an instance.
        private PersonBuilder() {
        }

        public PersonBuilder surname(final String surname) {
            this.surname = surname;
            return this;
        }

        public PersonBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public PersonBuilder day(final String day) {
            this.day = day;
            return this;
        }

        public PersonBuilder car(final String car) {
            this.car = car;
            return this;
        }

        public Person build() {
            return new Person(this);
        }
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getDay() {
        return day;
    }

    public String getCar() {
        return car;
    }
}