UNC COMP 301 - F26/Lec 20 - MVC (Cont.) - Songs Playlist example
Overview
Muhammad Sayeed Ghani walks through a JavaFX songs playlist application to show how MVC divides playlist data, user-request handling, and interface rendering. The design uses Song and playlist model interfaces, an Observer pattern to refresh views after model changes, and an alternate MVC architecture where views call a controller that validates input and delegates operations to the model.
Key takeaways
- The playlist model should expose a generic move-from-index-to-index operation, while the controller translates view-specific move-up and move-down requests into index changes.
- The concrete model must own a copy of its initial ArrayList so that playlist mutations do not alter the caller's predefined song list.
- Model-level validation protects data integrity by rejecting ratings outside 0 to 5 with an exception; controller-level validation can instead guide the user to correct input.
- Observer notifications belong after playlist mutations such as add, remove, move, and shuffle, not after read-only operations such as retrieving a song count.
- In alternate MVC, JavaFX views call the controller and never access the model directly, while the controller delegates data operations to the model.
- A JavaFX view hierarchy can combine a VBox for the overall window, a StackPane for a single centered control, and HBoxes for individual song rows.
Chapters
- The playlist example is a larger MVC application intended to finish the course's originally scheduled topics.
- The walkthrough starts with the running JavaFX application and then examines its model, controller, view, and App wiring.
- The code is reviewed rather than written live because the application is large enough that live coding would take too long.
- The JavaFX window opens with a predefined playlist containing songs such as "Africa."
- Users can add songs with a title, artist, and rating, or delete an existing song.
- Up and down arrow controls move individual songs, while Shuffle randomizes the playlist order.
- A user-visible change is first noticed by the view, interpreted by the controller, and applied to data by the model.
- The code walkthrough follows model, controller, then view, while revisiting the App class to explain how components are connected.
- The roadmap distinguishes each module's responsibility instead of treating the playlist window as a single block of code.
- In classic MVC, the view receives both the controller and model and can observe or access the model directly.
- In alternate MVC, the view receives only the controller, keeping the view and model separated.
- The model is the practical starting point for implementation because both the controller and view depend on its data and operations.
- A Song is defined by three attributes: title, artist, and rating.
- The interface needs getters for those attributes so the view can display a song.
- The demonstrated UI has no controls for editing an existing song's fields, so setters are unnecessary; construction supplies all three values.
- The concrete song class stores title, artist, and rating as private fields and can make them final.
- Its constructor accepts all three attributes and checks that the rating is within the allowed 0-to-5 range.
- Invalid model data triggers an exception, while getters implement the Song interface.
- The playlist model interface includes adding a Song, getting the song count, retrieving a song by index, and removing a song by index.
- A generic move operation accepts source and destination indices instead of exposing view-specific move-up and move-down methods.
- The interface also provides shuffle and observer registration operations.
- The playlist model acts as the Observer pattern's subject and maintains observers that need updates when playlist data changes.
- A ModelObserver interface defines an update method that receives a reference to the Model.
- The observer can use that model reference to inspect the current playlist after a change.
- The concrete model keeps one list of Song objects and another list of ModelObserver objects.
- Overloaded constructors support either a supplied initial playlist or an empty playlist.
- Copying the supplied songs into a new ArrayList gives the model its own mutable list rather than sharing the caller's list.
- The JavaFX App class implements start and creates the initial song list before constructing the concrete model.
- InitialPlaylist.create builds a static list using the concrete song implementation and title, artist, and rating values.
- InitialPlaylist resembles a singleton through static state and a static creation method, but it is not a singleton because it lacks a private constructor.
- Adding or removing a song changes the model and is followed by notifying observers; read-only getters do not require notifications.
- Moving a song removes it from its old index and inserts it at the new index, shifting intervening ArrayList elements as needed.
- The model uses Collections.shuffle for playlist randomization and then notifies its observers.
- Controller methods are shaped around actions the view can request, rather than simply duplicating every model method.
- Adding a song accepts separate title, artist, and rating values from the form, then constructs a Song for the model.
- The view can request move-up or move-down actions, while the model retains a generic move-from-index-to-index operation.
- The controller calls the model directly; an Observer relationship is useful for one subject notifying multiple observers, not for this single controller-to-model call.
- Under alternate MVC, the controller stores a Model reference but does not need a View reference.
- Move-up checks that an index is greater than zero, then delegates a move from index to index minus one; move-down checks the playlist boundary and uses index plus one.
- The controller validates user-supplied ratings and can respond gracefully, while the model independently enforces data integrity by throwing an exception for invalid ratings.
- The controller handles model exceptions because it is the layer that can translate failures into a useful response for the user.
- Each view component implements FXComponent, whose render method returns the root Parent of its JavaFX scene graph.
- The main View stores a controller and assembles a VBox with controls at the top, the playlist in the middle, and the add-song form at the bottom.
- The playlist is built by asking the controller for the song count and creating a SongView for each index.
- The view follows alternate MVC by receiving only the controller, not a direct model reference.
- ControlsView renders the Shuffle button in a StackPane, which centers its single child, and its action calls the controller's shuffle method.
- SongView stores a controller and song index, then renders a horizontal row containing delete, up, and down buttons plus song details.
- The delete button uses a Unicode X character and calls remove-song with that row's index; arrow buttons call the controller's move-up and move-down methods.
- The lecture ends before the remaining SongView helper methods are covered, leaving the rest of the view implementation for the next class.
Transcript excerpt
0:01 today. Uh well hopefully everybody is well on their way to getting assignment number eight part one completed. Uh so what we're going to discuss today and perhaps continuing on with this lecture in the next uh on Tuesday we should be finishing up essentially the original scheduled uh topics for this course. And so that's the good news. Um while we have missed a couple of lectures however um we have actually covered essentially the entire course uh by the end of this lecture or perhaps Tuesday however um we would have still have a few lectures left so I haven't quite decided what topics I'm going to cover in those but I'll probably
0:49 introduce a few topics which might be a bit more relevant for you uh once you go into the industry after a few years hopefully um or perhaps Perhaps I'll go back and take a look at some of the topics that we perhaps didn't spend as much time on uh in the in the early lectures and focus on those. But we'll see. So today um we'll go on and take a look at a larger slightly larger uh model view controller application which is a songs playlist. All right. And what we'll do initially is take a look at the actual code uh well actually at the running code and see if we can understand that and once we have an idea of how the application actually runs and
1:37 the user interface we'll go ahead and um walk through the entire code. Now I'm not going to do any live coding. This is uh that would take far too long and it probably introduce a lot of errors. Uh it is pretty large piece of code. uh what I'm simply going to do is going to
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Muhammad Sayeed Ghani.