https://github.com/tharindu714/flyweight-music-streaming-application
MelodyShare is a compact, colourful demo that uses the Flyweight design pattern to share heavy album data (artwork, artist, album meta) across many Song instances. This reduces memory usage while allowing thousands of simultaneous plays.
https://github.com/tharindu714/flyweight-music-streaming-application
design-patterns flyweight-pattern java oodp
Last synced: 10 months ago
JSON representation
MelodyShare is a compact, colourful demo that uses the Flyweight design pattern to share heavy album data (artwork, artist, album meta) across many Song instances. This reduces memory usage while allowing thousands of simultaneous plays.
- Host: GitHub
- URL: https://github.com/tharindu714/flyweight-music-streaming-application
- Owner: Tharindu714
- License: mit
- Created: 2025-08-15T04:29:07.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-08-15T04:44:41.000Z (10 months ago)
- Last Synced: 2025-08-15T06:21:55.243Z (10 months ago)
- Topics: design-patterns, flyweight-pattern, java, oodp
- Language: Java
- Homepage:
- Size: 214 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ต Flyweight Music Streaming Application โ MelodyShare
**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 `Song` instances. This reduces memory usage while allowing thousands of simultaneous plays.
---
## โจ Highlights
* ๐ฏ 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.
---
## ๐ Features
* 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 `Album` reuse (`s1.album == s2.album`).
---
## ๐ ๏ธ Build & Run
1. Clone the repo:
```bash
git clone https://github.com/Tharindu714/Flyweight-Music-Streaming-Application.git
cd Flyweight-Music-Streaming-Application
```
2. Compile & run the single-file demo (Java 8+):
```bash
javac MusicPlayer_Flyweight.java
java MusicPlayer_Flyweight
```
> The app prints a short console demo and opens the MelodyShare GUI.
---
## ๐งญ Design Overview
**Classes**
* `Album` โ *Flyweight* (intrinsic state): `albumName`, `artist`, `artwork` (simulated heavy byte\[]). Immutable.
* `AlbumFactory` โ *FlyweightFactory*: returns shared `Album` instances keyed by album+artist. Simple pool.
* `Song` โ *Context/Extrinsic state*: `title`, `playbackPosition`, `playlistName` + reference to `Album`.
* `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.
---
## ๐ UML (PlantUML)
---
## โ
Anti-patterns avoided (rules applied)
* **No per-instance heavy resources**: artwork stored once per album, not per song.
* **Immutability for shared data**: `Album` is immutable โ prevents accidental shared-mutable-state bugs.
* **Avoid global unbounded caches in production**: `AlbumFactory` is simple; in a real system consider `ConcurrentHashMap`, 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.
---
## ๐งช Example console output (from `main`)
```
s1.album == s2.album ? true
Number of unique Album flyweights: 2
```
---
## ๐ธ Screenshots / Uploads (placeholders)
* **Question screenshot** (upload here):
---
## ๐ง Extending & Production Notes
* Make `AlbumFactory` thread-safe with `ConcurrentHashMap` and consider `WeakReference` values 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.
---
## ๐ฎ Contribution
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. ๐ง