I was trying to sharp my OOD thinking. Here is my question.
Suppose you want to design a music mp3 player. And I got a class with a collection of playlists.
class Player {
Map<String, List<Song>> playlists; // <Name, PlayList>
public void play (Song song) {
// decode song
// play song
}
public void play (String playlistName) {
// play a playlist
for (Song song : playlists.get(playlistName)) {
play (song);
}
}
public void stop () {
// stop playing
}
public void pause () {
// pause, resume playing the last song when hit play again
}
}
Let’s assume “Song” already contains all the metadata of a song. All the functionalities of methods have been described. I got stucked when I was trying to realize the “pause” method. What would you do to realize this?
Thanks!
Look into state pattern. Which you will store state of the player. When you hit pause and play you will know the state of the player.