Problem: Different C structs may require different functions to provide similar behavior. Hypothetical example:
typedef struct {
int keyNumber;
} MIDINote;
typedef struct {
double freq;
} DSPNote;
MIDINote *aMidiNote;
DSPNote *aDSPNote;
/* ... (create and fill in fields of structs) */
playMIDI(aMidiNote);
playDSP(aDSPNote);
|
We'd prefer a similar behavior to be represented by a single "message". Hypothetical example:
play(aMidiNote); play(aDSPNote); |
But this requires the writer of play to know every possible kind of Note it would be passed. This violates the principle of programming modularity.
Objective-C lets each Class define its own play "method". The Objective-C run-time system then invokes the correct play method. This process is called "messaging".