The Leaky Collection Problem
The YouTubePlaylist does not protect its own data. It hands the whole internal list to the client. The class then loses control of its state, and the client must work out how to walk through the list.
1. Understanding the Issue
In the code, YouTubePlaylist is a thin wrapper around a List<Video>. Its getVideos() method returns that internal list. This is a Leaky Abstraction: a detail that should be hidden leaks out. The client is no longer just using the playlist. It is handling the playlist's storage.
2. What are the Issues?
Exposes Internal Structure
- The Issue:
getVideos()returns the internal list itself. - The Impact: This breaks encapsulation (a class keeps its data private). A client can change the list from outside, for example with
.clear()or.remove(). This skips any business rules or checks the class has.
Tight Coupling with Underlying Structure
- The Issue: Outside code depends on the exact collection type (like
ArrayList,Vector, orLinkedList). - The Impact: Say you switch to a
Mapfor faster lookups or aPriorityQueuefor sorting. Every piece of client code that callsgetVideos()will break.
No Control Over Traversal
- The Issue: The code that walks through the list lives outside the class.
- The Impact: To add shuffle play, reverse order, or hiding private videos, you must edit every client. The walking logic is scattered everywhere.
Difficult to Support Multiple Independent Traversals
- The Issue: No object tracks the "current position" of a scan.
- The Impact: If two parts of your program read the same playlist at the same time, each must keep its own index by hand. This repeats code and invites mistakes.
3. Structural Analysis
| Component | Status | Observation |
|---|---|---|
| Encapsulation | Broken | Internal List is public property via the getter. |
| Client Code | Over-burdened | Client must know how to loop through a List. |
| Flexibility | Low | Changing the data structure requires a full refactor. |
4. Summary of the "Smell"
When a class says "Here is my internal list, go loop through it yourself," it is giving up its job. A good collection lets you get its elements one by one without showing how they are stored. The Iterator Pattern does this. It gives you a standard "Remote Control" to move through the collection.