A chain of getter calls that looks like a train wreck is a code smell – it's a symptom of insufficient encapsulation, like in this java example:

void breakfast() {
	if (!human.getBadMemories().contains(Sausage.class)) {
		human.getDigestionSystem().getPeritoneum()
				.getStomach().add(new Sausage(2));
	}
}

class Human {
	// only getters and setters
}

Introduce intermediary methods so that only direct collaborators are called:

void breakfast() {
	human.eat(new Sausage(2));
}

class Human {
	void eat(Food food) {
		if (dislikes(food)) {
			throw new WillNotEat(food);
		}
		digestionSystem.swallow(food);
	}
}

Good encapsulation is when the object hides its private parts completely and exposes just a couple of methods/commands to which it can react. Other objects can then focus on what they want from such properly encapsulated object (make it eat) rather than worry about its inner guts workings.

            Votes: 0

See more like this: