Implement Composite Design Pattern
parent
027a97aaae
commit
caa4e45f04
@ -0,0 +1,37 @@
|
|||||||
|
package composite;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args){
|
||||||
|
|
||||||
|
SongComponent musicFolder = new SongGroup("Goudham's Music Folder", "- A personal collection of songs that I enjoy");
|
||||||
|
|
||||||
|
SongComponent kpopMusic =
|
||||||
|
new SongGroup("Kpop",
|
||||||
|
"is a genre of popular music originating in South Korea.");
|
||||||
|
|
||||||
|
SongComponent electronicMusic =
|
||||||
|
new SongGroup("\nElectronic",
|
||||||
|
"is music that employs electronic musical instruments, digital instruments or circuitry-based music technology");
|
||||||
|
|
||||||
|
SongComponent jpopMusic =
|
||||||
|
new SongGroup("\nJpop",
|
||||||
|
"is a musical genre that entered the musical mainstream of Japan in the 1990s");
|
||||||
|
|
||||||
|
musicFolder.add(kpopMusic);
|
||||||
|
|
||||||
|
kpopMusic.add(new Song("Better", "Twice", 2020));
|
||||||
|
kpopMusic.add(new Song("Deja Vu", "Dreamcatcher", 2019));
|
||||||
|
|
||||||
|
musicFolder.add(jpopMusic);
|
||||||
|
|
||||||
|
jpopMusic.add(new Song("PA PA YA", "BabyMetal", 2019));
|
||||||
|
jpopMusic.add(new Song("Megitsune", "BabyMetal", 2013));
|
||||||
|
|
||||||
|
musicFolder.add(electronicMusic);
|
||||||
|
|
||||||
|
electronicMusic.add(new Song("Houndin", "Layto", 2020));
|
||||||
|
electronicMusic.add(new Song("Wrong", "Far Out", 2017));
|
||||||
|
|
||||||
|
musicFolder.displaySongInfo();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package composite;
|
||||||
|
|
||||||
|
public class Song extends SongComponent {
|
||||||
|
|
||||||
|
String songName;
|
||||||
|
String bandName;
|
||||||
|
int releaseYear;
|
||||||
|
|
||||||
|
public Song(String songName, String bandName, int releaseYear){
|
||||||
|
this.songName = songName;
|
||||||
|
this.bandName = bandName;
|
||||||
|
this.releaseYear = releaseYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSongName() { return songName; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getBandName() { return bandName; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getReleaseYear() { return releaseYear; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displaySongInfo(){ System.out.println(this); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Song{" +
|
||||||
|
"songName='" + songName + '\'' +
|
||||||
|
", bandName='" + bandName + '\'' +
|
||||||
|
", releaseYear=" + releaseYear +
|
||||||
|
"} ";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package composite;
|
||||||
|
|
||||||
|
public abstract class SongComponent {
|
||||||
|
|
||||||
|
public void add(SongComponent songComponent) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(SongComponent songComponent) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SongComponent getComponent(int componentIndex) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
public String getSongName() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
public String getBandName() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
public int getReleaseYear() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
public void displaySongInfo() { throw new UnsupportedOperationException(); }
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package composite;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SongGroup extends SongComponent {
|
||||||
|
|
||||||
|
private final List<SongComponent> songComponents = new ArrayList<>();
|
||||||
|
private final String groupName;
|
||||||
|
private final String groupDescription;
|
||||||
|
|
||||||
|
public SongGroup(String groupName, String groupDescription) {
|
||||||
|
this.groupName = groupName;
|
||||||
|
this.groupDescription = groupDescription;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SongIterator createIterator() { return new SongIterator(songComponents); }
|
||||||
|
|
||||||
|
public String getGroupName() {
|
||||||
|
return groupName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroupDescription() { return groupDescription; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(SongComponent songComponent) { songComponents.add(songComponent); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(SongComponent songComponent) { songComponents.remove(songComponent); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SongComponent getComponent(int componentIndex) { return songComponents.get(componentIndex); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displaySongInfo() {
|
||||||
|
System.out.println(getGroupName() + " " + getGroupDescription() + "\n");
|
||||||
|
|
||||||
|
SongIterator songIterator = createIterator();
|
||||||
|
while (songIterator.hasNext()) {
|
||||||
|
SongComponent songInfo = songIterator.next();
|
||||||
|
songInfo.displaySongInfo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package composite;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SongIterator implements Iterator<SongComponent> {
|
||||||
|
List<SongComponent> playList;
|
||||||
|
private int currentIndex = 0;
|
||||||
|
|
||||||
|
public SongIterator(List<SongComponent> playList) {
|
||||||
|
this.playList = playList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return currentIndex < playList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SongComponent next() {
|
||||||
|
if (this.hasNext()) {
|
||||||
|
return playList.get(currentIndex++);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue