An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# ๐ŸŽต Flyweight Music Streaming Application โ€” MelodyShare


image

**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)


UML


---

## โœ… 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):


Scenario_6

---

## ๐Ÿ”ง 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. ๐ŸŽง