Delegation
Delegation
(When not to use inheritance)
If you have written almost any non-trival code you have used delegation.
Look at the simple example below and this will become apparent.
The class Mp3Player ‘delegates’ the storage of the mp3 object
to the vector class, instead of extending the vector class.
-----------------------------------------------------
class Mp3Player{
Vector storage = new Vector();
public void saveMp3(Object mp3){
storage.add(mp3);
}
/*
This class would also have methods
to: play and delete (etc ...)
the mp3 objects
*/
}
-----------------------------------------------------
Here is the above java class.
delegation.java