The inspiration for this post came to me mainly because of the discussion in comments which I had on Sebastian’s Malaca blog. We had a little debate there about the famous “getters and setters” duo. In my opinion, you can peacefully write software, completely avoiding them (with the obvious exception for DTO’s). My interlocutors had a little different point of view – they stated, that although you should avoid them, there are some cases when G&S are a must. I came up with classic example of a Human, which we can image as:
public class Human {
private Legs legs;
private Heart heart;
private Brain brain;
public Human(Heart heart, Brain brain, Legs legs){
this.legs = legs;
this.heart = heart;
this.brain = brain;
}
public Distance walk(Place origin, Place destination) throws UnreachableDestinationException {
heart.accelerateRate(150);
Distance summaryDistance = new Distance(0, Unit.METER);
while(!origin.equals(destination)){
if(!brain.canMoveByStep()){
throw new UnreachableDestinationException(destination);
}
Distance stepDistance = legs.moveStep();
summaryDistance = summaryDistance.add(stepDistance);
origin = origin.move(stepDistance);
}
return summaryDistance;
}
public void talk(Human interlocutor){
//some code
}
public WorkResult work(Task task){
//some code
}
}
As we see in this example, you don’t have any getters or setters, all possible actions of a Human are expressed by the methods attached to its class. OK, everything looks beautiful, but what happens if our walking Human gets hit by a car ? If the injury is serious, he will be taken to the hospital. In the hospital he will be medically examined by some (let us hope) skilled medical personnel. If the injury was real worse, the situation may require to perform an operation on an open heart. Unfortunately our properly encapsulated Human doesn’t expose its internal state of which the heart is part of. So, what can (and does) a surgeon ? He just simply breaks the encapsulation by cutting the chest and gets his job done.
If we wanted to translate (model) the actions of a surgeon in Java code, we would write:
public class Surgeon {
private HashMap<String, MedicalProcedure> knownProcedures;
public Boolean operate(Human patient, String procedureName) throws FamilyDoesNotAllowException {
Field[] fields = Human.class.getDeclaredFields();
try {
List<Organ> organs = new ArrayList<Organ>();
for (Field field : fields) {
field.setAccessible(true);
Organ organ = (Organ)field.get(patient);
organs.add(organ);
}
MedicalProcedure procedure = knownProcedures.get(procedureName);
return procedure.execute(organs);
} catch (IllegalAccessException e) {
throw new FamilyDoesNotAllowException();
}
}
}
In the above example, Java’s reflection API was used to break the encapsulation of the instance of a Human class. If we dive into the body of the method “operate”, we could see that an exception could be thrown – the IllegalAccessException. This is because the JVM could be configured to ban such usages of the reflection API. In the real world, the family could also disagree to operate the patient.
So much for drawing parallels between the real world and Java code. As we see, we cannot relay on this particular feature of Java’s reflection API. If we want to save our innocent Human being, we have two options:
- write getters and setters for the organs
- add a new method to the Human class, to let him be operated
Let write some code to see, how could it be implemented: