Skip to content

Observer Pattern: Event-Driven Communication

Tell many objects when something changed

Builds on Interfaces and Coupling and Cohesion

Stage 3core2 min read

The Hard-Coded Notification Problem

In a naive implementation, the Subject (the YouTube Channel) has to manage its Observers (Subscribers) by hand. The result is code that is stiff and easy to break. The main job, uploading videos, gets buried under notification details.


1. Understanding the Issue

Imagine we’re building a simple YouTube-like Notification System. Whenever a creator uploads a new video, all their subscribers should get notified. In the current design, the YoutubeChannel class must know exactly who is subscribed and how to reach each person. In the code, uploadNewVideo prints an email line and an in-app line, one for each subscriber, written straight into the method.


2. What’s Wrong with This Approach?

Tightly Coupled Code

  • The Issue: The YoutubeChannel class itself decides how users are notified.
  • The Impact: If tomorrow we want to send an SMS or push notification, we have to edit the main uploadNewVideo method. The "What" (uploading) is glued to the "How" (notifying).

Violation of Single Responsibility Principle (SRP)

  • The Issue: The class does two jobs: it handles video uploads and it handles how users are contacted.
  • The Impact: The channel should only care about the video. Because it also handles email addresses and notification types, the class grows large and is hard to test.

Violation of Open/Closed Principle (OCP)

  • The Issue: The class has to be changed again and again. Every time a new user clicks the "Subscribe" button, a developer must add a new line of code to the YoutubeChannel class.
  • The Impact: Code should be open for extension but closed for modification, meaning you add features without editing working code. This design breaks that rule every time.

No Runtime Flexibility (Scalability Issues)

  • The Issue: Subscribers are hard-coded into the method.
  • The Impact: A user cannot "Unsubscribe" or change how they are notified while the program runs. With hundreds or thousands of users, you would be editing code all the time.

3. Structural Analysis

FeatureStatusObservation
DependencyRigidThe Channel depends on concrete email addresses.
LogicHard-codedNotifications are baked into the upload method.
MaintenanceHighEvery subscriber change requires a code deployment.
Dynamic BehaviorImpossibleUsers cannot join or leave while the app is running.

4. Summary of the "Smell"

A "smell" is a sign in the code that something is off. Suppose a method does its main job and then calls several unrelated services by hand (email, in-app messages, logging). That points to a missing subscription mechanism.

The Observer Pattern fixes this by allowing the Channel to say: "I just uploaded a video; I don't care who is listening, but here is the update." Each observer then handles the "how" on its own.


In this topic

Before code1 file

Before/Main.java39 linesGitHub
package DesignPatterns.BehaviouralPatterns.ObserverPattern.Before;/** * PROBLEM: Tight Coupling & Lack of Scalability. * The 'YoutubeChannel' (Subject) is directly responsible for knowing WHO * needs a notification and HOW to send it. */class YoutubeChannel {    public void uploadNewVideo(String videoTitle) {        System.out.println("Uploading new video: " + videoTitle);        /**         * ISSUE 1: HARD-CODED SUBSCRIBERS         * Every time a new person hits the 'Subscribe' button, you have to         * manually add a line of code here. This violates the Open/Closed Principle.         */        System.out.println("Sending email to rizon.kumar.rahi@gmail.com");        /**         * ISSUE 2: VARYING NOTIFICATION METHODS         * What if one user wants Email and another wants SMS?         * The YoutubeChannel class will soon be bloated with logic for         * various notification protocols, violating the Single Responsibility Principle.         */        System.out.println("Sending in-app notification to user@gmail.com");    }}public class Main {    public static void main(String[] args) {        YoutubeChannel channel = new YoutubeChannel();        // ISSUE 3: NO RUNTIME FLEXIBILITY        // There is no way for a user to "Unsubscribe" without deleting code.        channel.uploadNewVideo("Design Patterns in JAVA");    }}

Done with this topic?

How well do you know it?