Pessoal observando o código pude ver muitas classes de notas poderiam ser substituída por uma enum, reduzindo bastante a quantidade de código. Pode ser feito dessa forma?
package br.com.schimidtsolutions.estudo.flyweight;
import java.util.Arrays;
import java.util.List;
public class Programa {
public static void main(final String[] args) {
List<NotaMusical> musica = Arrays.asList(NotaMusical.getInstance("do").get(),
NotaMusical.getInstance("re").get(), NotaMusical.getInstance("mi").get(),
NotaMusical.getInstance("fa").get(), NotaMusical.getInstance("fa").get(),
NotaMusical.getInstance("fa").get(), NotaMusical.getInstance("do").get(),
NotaMusical.getInstance("re").get(), NotaMusical.getInstance("do").get(),
NotaMusical.getInstance("re").get(), NotaMusical.getInstance("re").get(),
NotaMusical.getInstance("re").get(), NotaMusical.getInstance("do").get(),
NotaMusical.getInstance("sol").get(), NotaMusical.getInstance("fa").get(),
NotaMusical.getInstance("mi").get(), NotaMusical.getInstance("mi").get(),
NotaMusical.getInstance("mi").get(), NotaMusical.getInstance("do").get(),
NotaMusical.getInstance("re").get(), NotaMusical.getInstance("mi").get(),
NotaMusical.getInstance("fa").get(), NotaMusical.getInstance("fa").get(),
NotaMusical.getInstance("fa").get());
Piano piano = new Piano();
piano.tocar(musica);
}
}
package br.com.schimidtsolutions.estudo.flyweight;
import java.util.Optional;
public enum NotaMusical {
DO("C"), RE("D"), MI("E"), FA("F"), SOL("G"), LA("A"), SI("B");
private String simbolo;
private NotaMusical(final String simbolo) {
this.simbolo = simbolo;
}
public String getSimbolo() {
return simbolo;
}
public static Optional<NotaMusical> getInstance(final String nome) {
NotaMusical notaMusicalEncontrada = null;
String nomeEmMaiusculo = nome.toUpperCase();
for (NotaMusical notaMusical : values()) {
if (notaMusical.name().equals(nomeEmMaiusculo)) {
notaMusicalEncontrada = notaMusical;
}
}
return Optional.ofNullable(notaMusicalEncontrada);
}
}
package br.com.schimidtsolutions.estudo.flyweight;
import java.util.List;
import java.util.stream.Collectors;
import org.jfugue.Player;
public class Piano {
public void tocar(List<NotaMusical> musica){
String simbolosNotasMusicais = musica.stream()
.map(NotaMusical::getSimbolo)
.collect(Collectors.joining(" "));
Player player = new Player();
player.play(simbolosNotasMusicais);
}
}