Repository: https://github.com/Tharindu714/Flyweight-Music-Streaming-Application.git
MelodyShare is a compact, colourful demo that uses the Flyweight design pattern to share heavy album data (artwork, artist, album meta) across many
Songinstances. This reduces memory usage while allowing thousands of simultaneous plays.
- 🎯 Demonstrates the Flyweight Pattern (Album = flyweight, Song = context, AlbumFactory = flyweight factory).
- 💾 Simulates heavy artwork bytes per album and shows memory savings when albums are shared.
- 🎨 Attractive musical-themed Swing UI with gradient header, song list and live memory stats.
- 🛡️ Anti-pattern rules applied: no per-instance duplication of heavy resources, album objects immutable, and clear separation of responsibilities.
- Add single songs reusing albums.
- Auto-generate large batches of songs (e.g., add 1000 songs) to stress test the flyweight pool.
- Live stats: number of song instances, unique albums (flyweights), estimated memory saved (MB).
- Console demo shows
Albumreuse (s1.album == s2.album).
- Clone the repo:
git clone https://github.com/Tharindu714/Flyweight-Music-Streaming-Application.git
cd Flyweight-Music-Streaming-Application- Compile & run the single-file demo (Java 8+):
javac MusicPlayer_Flyweight.java
java MusicPlayer_FlyweightThe app prints a short console demo and opens the MelodyShare GUI.
Classes
Album— Flyweight (intrinsic state):albumName,artist,artwork(simulated heavy byte[]). Immutable.AlbumFactory— FlyweightFactory: returns sharedAlbuminstances keyed by album+artist. Simple pool.Song— Context/Extrinsic state:title,playbackPosition,playlistName+ reference toAlbum.MusicPlayerFrame— Swing UI to add songs and view stats.
Why Flyweight? Many songs share identical album data (artwork + metadata). Storing that data once per album and reusing it avoids massive duplication and reduces memory pressure.
---
- No per-instance heavy resources: artwork stored once per album, not per song.
- Immutability for shared data:
Albumis immutable — prevents accidental shared-mutable-state bugs. - Avoid global unbounded caches in production:
AlbumFactoryis simple; in a real system considerConcurrentHashMap, eviction, weak references or LRU to prevent memory leaks. - Separation of concerns: factory handles pooling, songs handle playback details.
- Thread-safety caution: the demo is single-threaded Swing; make the factory thread-safe in multi-threaded servers.
s1.album == s2.album ? true
Number of unique Album flyweights: 2
- Question screenshot (upload here):
- Make
AlbumFactorythread-safe withConcurrentHashMapand considerWeakReferencevalues to allow GC of unused albums. - Replace simulated
byte[]artwork with real image assets; load lazily and keep thumbnails in memory. - Add metrics + memory profiling to verify actual savings.
Fork, add features (e.g., async loading, album eviction, real artwork), and send PRs. Include tests showing memory usage improvements.
Made with ♫ by Tharindu — enjoy and share! Keep the music playing. 🎧

