Procedural code gets information then
makes decisions. Object-oriented code
tells objects to do things. — Alec Sharp
As soon as you start developing software where you get all the information you need, you have stopped developing Object-Oriented Software. Consider:
public class Dog {
private VoiceBox voiceBox = new VoiceBox();
public VoiceBox getVoiceBox() {
return this.voiceBox;
}
}
It is absurd to write the following:
Dog sparky = new Dog();
VoiceBox voiceBox = sparky.getVoiceBox();
voiceBox.makeSound();
Why does any other class in the system need to use the Dog's voice box, or even need to know about it? If this was real life, you would not first try to extract the voice box from the dog before using it. Rather, you would do:
Dog sparky = new Dog();
dog.bark();
Using:
public class Dog {
private VoiceBox voiceBox = new DogVoiceBox();
private VoiceBox getVoiceBox() {
return this.voiceBox;
}
public void bark() {
VoiceBox voiceBox = getVoiceBox();
voiceBox.makeSound();
}
}
Why does another class need the race? To make decisions? If so, then:
public class Player{
private Race race = new Race();
public boolean isRace( RaceIdentifier ri ) {
return getRace().isRace( ri );
}
private Race getRace() {
return this.race;
}
}
public class Race {
public boolean isRace( RaceIdentifier ri ) {
return getRaceIdentifier().equals( ri );
}
}
Then:
Player gandalf = new Player();
if( gandalf.isRace( WIZARD ) ) {
((Wizard)gandalf).vanish();
}
The above code, no matter how race changes, never needs to change. The class encompassing the above snippet need never know about the Race class, effectively decoupling it from the rest of the system. When the Race needs to change (different type, more added, etc.), no other class is affected.
To me, this is the heart of OOP.