From caa4e45f04f1129ab3d0d32897c87615670ab753 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 22 Dec 2020 05:52:56 +0000 Subject: [PATCH] Implement Composite Design Pattern --- Design Patterns/src/composite/Main.java | 37 ++++++++++++++++ Design Patterns/src/composite/Song.java | 35 +++++++++++++++ .../src/composite/SongComponent.java | 22 ++++++++++ Design Patterns/src/composite/SongGroup.java | 44 +++++++++++++++++++ .../src/composite/SongIterator.java | 26 +++++++++++ 5 files changed, 164 insertions(+) create mode 100644 Design Patterns/src/composite/Main.java create mode 100644 Design Patterns/src/composite/Song.java create mode 100644 Design Patterns/src/composite/SongComponent.java create mode 100644 Design Patterns/src/composite/SongGroup.java create mode 100644 Design Patterns/src/composite/SongIterator.java diff --git a/Design Patterns/src/composite/Main.java b/Design Patterns/src/composite/Main.java new file mode 100644 index 0000000..1465880 --- /dev/null +++ b/Design Patterns/src/composite/Main.java @@ -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(); + } +} diff --git a/Design Patterns/src/composite/Song.java b/Design Patterns/src/composite/Song.java new file mode 100644 index 0000000..60bba0c --- /dev/null +++ b/Design Patterns/src/composite/Song.java @@ -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 + + "} "; + } +} diff --git a/Design Patterns/src/composite/SongComponent.java b/Design Patterns/src/composite/SongComponent.java new file mode 100644 index 0000000..13795c0 --- /dev/null +++ b/Design Patterns/src/composite/SongComponent.java @@ -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(); } +} \ No newline at end of file diff --git a/Design Patterns/src/composite/SongGroup.java b/Design Patterns/src/composite/SongGroup.java new file mode 100644 index 0000000..e602ce9 --- /dev/null +++ b/Design Patterns/src/composite/SongGroup.java @@ -0,0 +1,44 @@ +package composite; + +import java.util.ArrayList; +import java.util.List; + +public class SongGroup extends SongComponent { + + private final List 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(); + } + } +} diff --git a/Design Patterns/src/composite/SongIterator.java b/Design Patterns/src/composite/SongIterator.java new file mode 100644 index 0000000..489b472 --- /dev/null +++ b/Design Patterns/src/composite/SongIterator.java @@ -0,0 +1,26 @@ +package composite; + +import java.util.Iterator; +import java.util.List; + +public class SongIterator implements Iterator { + List playList; + private int currentIndex = 0; + + public SongIterator(List 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; + } +}