Трудно ответить на вопрос Как сделать все хорошо?
:)
Да и вариантов масса может быть при такой поверхностной формулировке.
Но я бы сделал так:
@Data
public class Disk {
private List<Playlist> playlists;
}
@Data
public class Playlist {
private List<Song> songs;
}
abstract public class Song {
abstract public String getArtist();
abstract public String getTitle();
abstract public int getDuration();
abstract public String getGenre();
}
@Data
abstract public class BaseSong extends Song {
private final String artist;
private final String title;
private final int duration;
public BaseSong(String artist, String title, int duration) {
this.artist = artist;
this.title = title;
this.duration = duration;
}
@Override
public String getGenre() {
return this.getClass().getSimpleName();
}
}
public class Rock extends BaseSong {
public Rock(String artist, String title, int duration) {
super(artist, title, duration);
}
}
public class Jazz extends BaseSong {
public Jazz(String artist, String title, int duration) {
super(artist, title, duration);
}
}
public class Performer extends Song {
private Queue<Song> queue = new ConcurrentLinkedQueue<>();
private volatile Song currentSong;
public Performer() {}
public Performer(Song song) {
this(Collections.singletonList(song));
}
public Performer(Collection<Song> songs) {
queue.addAll(songs);
}
public void add(Collection<Song> songs) {
queue.addAll(songs);
}
public void add(Song song) {
add(Collections.singletonList(song));
}
public void remove(Collection<Song> songs) {
queue.removeAll(songs);
}
public void remove(Song song) {
remove(Collections.singletonList(song));
}
public void play() throws InterruptedException {
while (!queue.isEmpty()) {
Song song = queue.poll();
setCurrentSong(song);
// Playing...
Thread.sleep(song.getDuration());
}
}
private void setCurrentSong(Song song) {
synchronized (this) {
currentSong = song;
}
}
public Song getCurrentSong() {
synchronized (this) {
if (currentSong == null) {
throw new IllegalStateException("No song is playing");
}
return currentSong;
}
}
@Override
public String getArtist() {
return getCurrentSong().getArtist();
}
@Override
public String getTitle() {
return getCurrentSong().getTitle();
}
@Override
public int getDuration() {
return getCurrentSong().getDuration();
}
@Override
public String getGenre() {
return getCurrentSong().getGenre();
}
}
@Data
- аннотация Lombok, чтобы не писать геттеры/сеттеры.