使用一个带参数的getter而不是多个没有参数的getter

时间:2015-03-06 06:46:30

标签: java oop getter

我第一次尝试做一些不是简单练习的OOP就是在java中创建一个存储人员信息的程序(比如联系簿)。下面是一个名为Person的类的片段,我正在研究它。在将来的某个时间,人将被其他一些类实例化。

public class Person {
    // Enum of the fields in Person
    private static enum Field {NAME, ALIASES, DATE_OF_BIRTH, DATE_OF_DEATH, VITAL_STATUS,
        RELATIONSHIPS}
    private static enum VitalStatus {ALIVE, DECEASED, ASSUMED_DECEASED, UNKNOWN}

    private final int id;
    private Name name;
    private List<String> aliases;
    private GregorianCalendar dateOfBirth;
    private GregorianCalendar dateOfDeath;
    private VitalStatus vitalStatus;
    private List<Relationship> relationships;

    // Time field was last updated
    private Map<Field, GregorianCalendar> updateTimes;  

    // Initialise a blank/unknown person
    public Person() {
        this.id = getNewId();
        this.name = new Name(); // Blank name
        this.aliases = new ArrayList<String>();
        this.dateOfBirth = null;
        this.dateOfDeath = null;
        this.vitalStatus = VitalStatus.UNKNOWN;
        this.relationships = new ArrayList<Relationship>();

        // Initialise all update times to null
        this.updateTimes = new HashMap<Field, GregorianCalendar>();
        for(Field f : Field.values()) {
            updateTimes.put(f, null);
        }
    }


    // Update time methods
    private void updateUpdateTime(Field field) {
        updateTimes.put(field, new GregorianCalendar());
    }

    public GregorianCalendar getNameUpdateTime() {
        return updateTimes.get(Field.NAME);
    }

    public GregorianCalendar getAliasesUpdateTime() {
        return updateTimes.get(Field.ALIASES);
    }

    public GregorianCalendar getDateOfBirthUpdateTime() {
        return updateTimes.get(Field.DATE_OF_BIRTH);
    }

    public GregorianCalendar getDateOfDeathUpdateTime() {
        return updateTimes.get(Field.DATE_OF_DEATH);
    }

    public GregorianCalendar getVitalStatusUpdateTime() {
        return updateTimes.get(Field.VITAL_STATUS);
    }

    public GregorianCalendar getRelationshipsUpdateTime() {
        return updateTimes.get(Field.RELATIONSHIPS);
    }

    // Code removed from here down
}

我正在考虑使用下面显示的代码替换更新时间的getter。

public getUpdateTime(Field field) {
    return updateTimes.get(field);
}

我应该更换吗?我对使用参数制作一个getter犹豫不决,但它会减少代码行数,并允许我添加新字段和更新时间,而无需编写新的getter。

2 个答案:

答案 0 :(得分:6)

在这种情况下,您可能希望将其添加为选项,但我会建议您反对它。使用了java.util.Calendar API(您指定要获取或更新的字段编号)以及Joda Time和java.time的API,您只需说getHour(),{{ 1}}等,后者很多更易于使用。

忽略添加新字段所需的代码行数 - 专注于使用您提供的API的难易程度。呼叫者是否需要对字段进行概括?如果是这样,那么有额外的方法是有道理的。如果不是 - 如果呼叫者总是在编译时确切地知道他们想要在呼叫站点中找到哪些值,那么我会坚持使用原始表格。

我确实理解你的情况与日历的情况略有不同,因为你对多个字段有相同的“方面”(最后更新时间),但我仍然对它的情况有所保持谨慎。来电者的观点。

(一般来说,我在getter中有一个参数也没有问题。做任何事情都会导致最有用的API。)

顺便说一下,如果可能的话,我会远离getMonth() - 您真的希望呼叫者能够更改上次更新时间吗?他们现在可以,因为GregorianCalendar是可变的。切换到Joda时间或GregorianCalendar如果可能可以。

答案 1 :(得分:1)

拥有setter和getter意味着该类可以满足作为JavaBean的要求

http://en.wikipedia.org/wiki/JavaBeans

此链接描述了编写javaBeans的优点,但也包括Spring,Stripes等框架的可操作性。

使用Eclipse之类的IDE可以轻松创建setter和getter方法。