UNC COMP 301 - F26/Lec 21 - MVC Playlist Views; SOLID Design Principles
Overview
The lecture completes a JavaFX song-playlist MVC walkthrough, showing how the views, controller, model, and app are wired and why the implementation resembles the alternate MVC architecture without following it exactly. It then introduces SOLID principles, explains Single Responsibility Principle (SRP) through a report-card example, and begins Open-Closed Principle (OCP) with a ticket-pricing example.
Key takeaways
- The playlist app resembles alternate MVC because MainView holds a controller reference and the controller holds a model reference, but the app class itself registers a model-observer lambda that rerenders the view.
- Rebuilding all SongView objects after every model change scales poorly: four songs and two button clicks produce 12 SongView objects, while a 100-song playlist rebuilds all 100 rows for a single move.
- AddSongView avoids unnecessary work by caching its static HBox layout, whereas dynamic playlist rows must reflect changes to song order and contents.
- The current add-song flow accepts empty title and artist values, so validation should be added, with the controller identified as a practical place to provide a user-facing response.
- SRP is about grouping code by responsibility and reasons for change, not limiting a class to one method; GradeBook, ReportFormatter, and ReportSaver separate grading, presentation, and persistence.
- The TicketPricer violates OCP because each new customer category, such as veterans, requires editing existing conditional logic, increasing the risk of regressions.
Chapters
- Every view implements the FX component interface, whose render method returns the root node for its scene graph.
- MainView arranges ControlsView, the playlist of songs, and AddSongView vertically, with each song represented by its own SongView.
- The lecture shifts from completing the playlist MVC walkthrough to introducing SOLID design principles.
- MainView stores a controller reference but no model reference, which points toward the alternate MVC architecture.
- A VBox holds the controls, song list, and add-song form; each child view's render method supplies a node to add to the layout.
- MainView asks the controller for the song count, then creates one SongView per playlist index.
- ControlsView uses a StackPane to center its single Shuffle button, a simple layout choice that would overlap multiple buttons.
- The button's JavaFX setOnAction lambda ignores its event argument and calls controller.shuffleSongs().
- The controller forwards the shuffle operation to the model, keeping the view from directly changing model data.
- Each SongView receives a controller and a song index, then uses an HBox to arrange one song row horizontally.
- Delete, move-up, and move-down buttons call controller methods with the song index.
- Helper methods retrieve the title, artist, and rating through the controller and render ratings as filled and empty stars.
- AddSongView checks whether its HBox layout already exists and reuses it rather than rebuilding the form on every render.
- The form is static, unlike playlist rows whose contents and positions may change after a delete, move, or shuffle.
- Its controls include title and artist text fields, labels, and a rating slider with a 0-to-5 range and default value of 5.
- The plus-button lambda reads title and artist text and the integer value of the rating slider.
- The controller's addSong method accepts the three user-entered values, constructs a Song object, and passes it to the model.
- After submission, the title and artist text fields are cleared.
- The app extends JavaFX Application, creates an initial playlist, passes it to the model implementation, then constructs the controller with that model.
- The view receives only the controller, while the controller receives the model, reinforcing the alternate architecture's reference structure.
- The app obtains the scene root from MainView.render(), applies main.css, sets the stage, and displays it.
- The app registers a lambda as an observer of the model; when the model changes, the lambda calls MainView.render() and updates the scene root.
- This wiring differs from both reference architectures: the observer is neither the view nor the controller, but a lambda in the app class.
- The JavaFX framework calls the app's start method after launch, an example of inversion of control that the course will revisit.
- The UML diagram shows the controller referencing the model and the view referencing the controller, with no direct view-to-model reference.
- With four songs, the initial render creates four SongView objects; two button-triggered re-renders create eight more, for 12 total.
- The current render approach reconstructs every song row after a change, even when only one or two rows need updating.
- For a playlist of 100 songs, moving one song still rebuilds all 100 rows; tracking changes and updating only affected components would be more efficient.
- Submitting an empty title and artist currently adds an empty song with the slider's default rating of 5; the controller is suggested as a suitable validation point.
- The implementation is closer to alternate MVC because view actions reach the model through the controller, but its app-level observer differs from the architecture's two-observer arrangement.
- Design patterns provide templates for particular problems, while design principles are general guidelines for object-oriented design.
- Robert Martin, also known as Uncle Bob, assembled the SOLID acronym; Bertrand Meyer is associated with OCP and Barbara Liskov with LSP.
- SOLID expands to Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion principles.
- The Single Responsibility Principle says a class should have one reason to change.
- The example ReportCard class stores student scores, calculates their average, formats the report, and saves it.
- Grade rules, report presentation, and storage can change for different reasons, such as dropping the lowest score, switching to HTML, or saving to a database.
- Low coupling means few dependencies between modules; high cohesion means a class groups methods and data that belong together.
- The example separates grading policy and scores into GradeBook, presentation into ReportFormatter, and persistence into ReportSaver.
- ReportFormatter needs access to GradeBook data, while ReportSaver can save the formatted text to a file.
- Methods that change together and serve one responsibility should remain in the same class; unrelated responsibilities should be separated.
- SRP does not require one method per class, and dividing code into too many classes can create unnecessary file and package clutter.
- MVC is a clear SRP example because the model handles data, the view handles presentation, and the controller handles application interactions.
- The Open-Closed Principle says software entities should be open for extension but closed for modification.
- The TicketPricer example gives students a 50% discount, senior citizens a 30% discount, and other customers the base price of $20.
- Adding a veterans' discount requires modifying the existing conditional logic, and string-based categories can also fail silently when a value such as Student is capitalized differently.
Transcript excerpt
0:00 So we're getting close to the end of the semester. So by the time we're done with our lecture today, we should have covered essentially all the key topics that were there in the original course outline. So after that, we don't have too many lectures left. I intend to finish off before Thanksgiving. So that means we have two more lectures left. So I'll I want to spend some time looking at another topic which isn't there in the course outline, but but I thought that would be pretty useful set of design principles that would help us in sort of reviewing the course and looking back back at the
0:49 at the design patterns that we've already seen. And so after we done with the song playlist walkthrough, which hopefully shouldn't take too long, we'll start off our discussion on what I refer to as the solid design principles, which are a set of five design principles. We'll talk about how they differ from the design patterns that we've already been talking about. So let's start off taking a final look at the model view controller. And we have already completed the model, walkthrough of the model and walkthrough of the controller. We'll continue on and take a look at where we left off from on on the view module. I'll do a bit of a review
1:37 and then we'll go on and start talking about design principles. So let's continue from the interface. So this is just a quick review. As in the last lecture, we spoke about that all views are going to be implementing the FX component, which has a single method, which the render method, which is draws out the components on the screen. If you go on and take a look at the main
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.