From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Challenge: the Composite pattern

Challenge: the Composite pattern

In this challenge, you'll need to implement the composite pattern for an app for playing the notes of a song. The songs are composites made up of musical note objects. So to explain how this app works, first there's a class called Notes, which takes a char as an argument in the constructor, and this is the value of the notes. Then there's also a method called play, which prints out the value of that note. And then there's a class called Song, which contains a list of notes. And there's a method called addNote, which adds a note to the list. And then there's a method called getNotes, which returns the list of notes. Finally, there's the App class, and this has a main method in it. So at the top of this method, all the possible notes are added from A to G. And then there's a new song variable, which is a song called doReMe. Then all the notes for the song are added using the addNote method. Then to play the song, at the moment, we have to get each note individually and call the play…

Contents