DEV Community

NOOB
NOOB

Posted on

LLD-6:YouTube Subscription System

YouTube Subscription System - Observer Pattern Implementation

This is an implementation of the Observer design pattern for a YouTube-style subscription system demonstrating many-to-many relationships where multiple users can subscribe to multiple channels.

Problem Statement

Designing the notification backend for YouTube where multiple channels (Subjects) can have multiple subscribers (Observers), and each channel maintains its own independent subscriber list. When a channel uploads a video, only its subscribers should be notified - not subscribers of other channels.

Key Challenge: Managing independent notification lists for multiple subjects where:

  • Shiv subscribes to MrBeast
  • Tina subscribes to MrBeast AND GFG
  • Laxmi subscribes to T-Series
  • When MrBeast uploads, only Shiv and Tina get notified (not Laxmi)

Class Diagram

        +------------------+             +-----------------------+
        |     Subject      |             |       Observer        |
        |   (Interface)    |             |      (Interface)      |
        +------------------+             +-----------------------+
        | + subscribe()    |             | + update()            |
        | + unsubscribe()  |             +-----------+-----------+
        | + notify()       |                         ^
        +--------+---------+                         |
                 ^                                   | (Implements)
                 | (Implements)                      |
                 |                                   |
        +--------+---------+             +-----------+-----------+
        |  YoutubeChannel  |<>---------->|      YouTubeUser      |
        +------------------+   (List)    +-----------------------+
        | - channelName    |             | - userName            |
        | - subscribers [] |             | + update(msg)         |
        | + uploadVideo()  |             +-----------------------+
        +------------------+
Enter fullscreen mode Exit fullscreen mode

Implementation

package observer.youtubesubscriptionsystem;

import java.util.ArrayList;
import java.util.List;

/**
 * YouTube Subscription System demonstrating Many-to-Many Observer Pattern.
 */
public class YoutubeSubscriptionSystem {

    /**
     * Observer interface for Users.
     */
    interface Observer {
        void update(String channelName, String videoTitle);
    }

    /**
     * Subject interface for Channels.
     */
    interface Subject {
        void subscribe(Observer observer);
        void unsubscribe(Observer observer);
        void notifySubscribers(String videoTitle);
    }

    /**
     * Concrete Subject representing a specific YouTube Channel.
     */
    static class YoutubeChannel implements Subject {
        private final String channelName;
        private final List<Observer> subscribers = new ArrayList<>();

        public YoutubeChannel(String channelName) {
            this.channelName = channelName;
        }

        @Override
        public void subscribe(Observer observer) {
            subscribers.add(observer);
            System.out.println("[System] New Subscriber added to " + this.channelName);
        }

        @Override
        public void unsubscribe(Observer observer) {
            subscribers.remove(observer);
            System.out.println("[System] Subscriber removed from " + this.channelName);
        }

        @Override
        public void notifySubscribers(String videoTitle) {
            for (Observer observer : subscribers) {
                observer.update(this.channelName, videoTitle);
            }
        }

        /**
         * Simulates a video upload and notifies subscribers.
         */
        public void uploadVideo(String videoTitle) {
            System.out.println("\n------------------------------------");
            System.out.println("Uploading to [" + this.channelName + "]: " + 
                             videoTitle);
            System.out.println("------------------------------------");
            notifySubscribers(videoTitle);
        }
    }

    /**
     * Concrete Observer representing a User.
     */
    static class YouTubeUser implements Observer {
        private final String userName;

        public YouTubeUser(String userName) {
            this.userName = userName;
        }

        @Override
        public void update(String channelName, String videoTitle) {
            System.out.println(" => [Notification] Hey " + userName + ", " + 
                             channelName + " just uploaded: \"" + videoTitle + "\"");
        }
    }

    public static void main(String[] args) {
        YoutubeChannel mrBeast = new YoutubeChannel("MrBeast");
        YoutubeChannel gfg = new YoutubeChannel("GFG");
        YoutubeChannel tseries = new YoutubeChannel("T-Series");

        Observer shiv = new YouTubeUser("Shiv");
        Observer tina = new YouTubeUser("Tina");
        Observer laxmi = new YouTubeUser("Laxmi");

        System.out.println("----Setting up Subscriptions----");

        // Shiv follows MrBeast
        mrBeast.subscribe(shiv);

        // Tina follows MrBeast AND GFG
        mrBeast.subscribe(tina);
        gfg.subscribe(tina);

        // Laxmi follows T-Series
        tseries.subscribe(laxmi);

        // --- SCENARIO 1: Uploads ---
        mrBeast.uploadVideo("I Built a Chocolate Factory!");
        gfg.uploadVideo("Observer pattern in 5 mins");
        tseries.uploadVideo("New Punjabi Song");

        // --- SCENARIO 2: Unsubscribe ---
        System.out.println("\n----Laxmi Unsubscribes from T-Series----");
        tseries.unsubscribe(laxmi);

        tseries.uploadVideo("Another Hit Song");
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Many-to-Many Relationship: Users can subscribe to multiple channels, channels can have multiple subscribers
  • Independent Subscriber Lists: Each channel maintains its own list of subscribers
  • Targeted Notifications: Only relevant subscribers receive notifications
  • Dynamic Subscriptions: Users can subscribe and unsubscribe at runtime
  • Personalized Notifications: Each notification includes both channel name and video title
  • Extensible: Easy to add new channels and users without modifying existing code

How It Works

  1. Multiple Subjects: Each YouTube channel is an independent subject with its own subscriber list
  2. Multiple Observers: Users can subscribe to any number of channels
  3. Subscription: Users call channel.subscribe(user) to start receiving notifications
  4. Upload & Notify: When a channel uploads via uploadVideo(), only its subscribers are notified
  5. Unsubscription: Users can call channel.unsubscribe(user) to stop receiving notifications

Sample Output

----Setting up Subscriptions----
[System] New Subscriber added to MrBeast
[System] New Subscriber added to MrBeast
[System] New Subscriber added to GFG
[System] New Subscriber added to T-Series

------------------------------------
Uploading to [MrBeast]: I Built a Chocolate Factory!
------------------------------------
 => [Notification] Hey Shiv, MrBeast just uploaded: "I Built a Chocolate Factory!"
 => [Notification] Hey Tina, MrBeast just uploaded: "I Built a Chocolate Factory!"

------------------------------------
Uploading to [GFG]: Observer pattern in 5 mins
------------------------------------
 => [Notification] Hey Tina, GFG just uploaded: "Observer pattern in 5 mins"

------------------------------------
Uploading to [T-Series]: New Punjabi Song
------------------------------------
 => [Notification] Hey Laxmi, T-Series just uploaded: "New Punjabi Song"

----Laxmi Unsubscribes from T-Series----
[System] Subscriber removed from T-Series

------------------------------------
Uploading to [T-Series]: Another Hit Song
------------------------------------
Enter fullscreen mode Exit fullscreen mode

Notice how after Laxmi unsubscribes, the last T-Series upload doesn't generate any notification!

Real-World Applications

  • YouTube subscription and notification system
  • Social media follow/follower systems (Twitter, Instagram)
  • Newsletter subscription platforms
  • Podcast notification systems
  • Blog RSS feed subscriptions
  • Event notification systems
  • Push notification services

Key Differences from Previous Examples

Unlike the Weather Station or Stock Market examples where there was typically one subject broadcasting to multiple observers, this system demonstrates:

  • Multiple independent subjects (channels)
  • Many-to-many relationships (users subscribe to multiple channels)
  • Isolated notification lists (each channel has its own subscribers)
  • Cross-subscription (same user can observe multiple subjects)

Top comments (0)