Complex Class Relationships Lab
Learning Goals
- Practice building complex class relationships
Introduction
In the previous lab, we built classes to represent Song
, Artist
and Genre
.
In this lab, we're going to expand on the previous solution. This time, we will
add albums as well as some additional behaviors for our Artist
class.
Instructions
For this application, individual songs will be used to keep track of its artist
and genre, just as before. The new Album
class will contain collections of
songs. The new Artist
class will also contain some additional behaviors, but
these should be added last, as they will rely on Album
.
-
The
Genre
class should take in one parameter,name
and assign it to a private property,_name
.Genre
should have a getter,name
that returns_name
. -
The
Artist
class should also take in one parameter,name
, and assign it to a private property,_name
.Artist
should have a getter and setter forname
to return or modify_name
. -
The
Song
class should take in three parameters, a title, an instance of an artist, and an instance of a genre. These should be assigned to three private properties,_title
,_artist
, and_genre
.Song
should have acaption
getter that takes the title, artist name and genre name and interpolates them into a single sentence.let queen = new Artist('Queen'); let popRock = new Genre('Pop Rock'); let dontStopMeNow = new Song("Don't Stop Me Now", queen, popRock); dontStopMeNow.caption; // => 'Don't Stop Me Now by Queen (Pop Rock)'
-
The
Album
class should take in a title, an instance of anArtist
and an array ofSong
instances, an assign these to private properties.Album
should have getters fortitle
,songs
, andartist
.- Write a
trackList
getter that returns an array of the album's song titles. - Write a
genres
getter that returns an array of genres from the album's list of songs. All array elements should be unique (no repeating genres).
- Write a
Expanding Artist
Once you have all tests passing for the Album
class, let's add a few more
features to Artist
:
- Add a private property,
_albums
, that starts as an empty array. - Create a getter and setter for
albums
that will either return an array ofAlbum
instances, or take in an array ofAlbum
s and assign it to the_albums
property. - Create a
discography
getter that returns the title of each album belonging to an artist - Create a
songs
getter that returns an array of allSong
instances from all albums belonging to an artist
Conclusion
With Object Orientation, we can build complex dependencies. These dependencies
may rely on classes one or two degrees removed (as we see getting songs for
an artist through albums). Dependencies can also be bi-directional - an
Album
can have an artist and an Artist
can have albums.
It is important to understand how to create these complex dependencies, but in general, it is recommended that you only create dependencies you absolutely need. The more complex your dependencies, the less flexible your classes will get (and therefor, the harder to change and maintain later on).
As we build more complex dependencies, we also need to make sure our data is kept consistent. In this case, albums have an artist and artists have many albums, but we needed to add additional code to make sure both are updated when an album is created.