Question

Maybe I made ​​a mistake with the choice of site.

Sorry for my English

Until recently, I thought that there was no point in writing setters and getters for a class which fields are set only once. Instead of the setters\getters I used public constant fields (or final in Java) and set fields via a constructor.

But I recently ran into a situation when this method proved to be very uncomfortable. Classes have many fields (5-7 fields either).

And I first realized the benefits of getters.

Instead of doing this:

class Human {
    public final int id;
    public final String firstName;
    public final String lastName;
    public final int age;
    public final double money;
    public final Gender gender;
    public final List<Human> children;

    Human(int id, String firstName, String lastName, int age, double money, Gender gender, List<Human> children) {
    // set fields here
    }
}


class HumanReader {
    Human read(Input input) {
        int id = readId(input);
        String firstName = readFirstName(input);
        // ...
        List<Human> children = readChildren(input);
        return new Human(id, firstName, lastName, age, money, gender, children);
    }
}

I began using the next solution:

interface Human {
    int getId();
    String firstName;
    // ...
    List<Human> getChildren();
}

class HumanImpl implements Human {
    public int id;
    public String firstName;
    // ...
    public List<Human> children;

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    // ...

    public List<Human> getChildren() {
        return children;
    }
}


class HumanReader {

    Human read(Input input) {
        HumanImpl human = new HumanImpl();
        human.id = readId(input);
        human.firstName = readFirstName(input);
        // ...
        human.children = readChildren(input);
        return human;
    }
}

I think that the second solution is better. It has not the complicated constructor with confusing order of parameters.

But what use are setters? I still can not understand. Or they need for uniformity?

Was it helpful?

Solution

The point of using setters and getters are to encapsulate the access to these values. So if your underlying data structure change, you will not have to change the rest of the code. Imagine for example that your Human class suddenly relies on a database...

But while the getters and setters offer a small protection against change, they still reflect very much the data structure beneath, so you are still likely to get in trouble. The best solution is to try to avoid getters and setters, and instead provide the services that the clients of the class actually need. (I.e. in the case of the human class, consider making the human able to present itself to an interface)

OTHER TIPS

Very quick explanation here, think of a situation where you need to change something else (maybe UI or data) everytime you change ( set ) a property. Then most easiest way of implementing this is doing it in a setter.

And here's a good list of reasons why you might want to use it,

Why use getters and setters?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top