> For the complete documentation index, see [llms.txt](https://jrp.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jrp.gitbook.io/notes/design-pattern/behavioural-observer-pattern.md).

# Behavioural - Observer Pattern

<div data-with-frame="true"><figure><img src="/files/6em670mYfMLzQy8RBzte" alt=""><figcaption></figcaption></figure></div>

Every time the YouTube **channel** (the Observable) uploads a new video, it wants to make sure its **subscribers** (the Observers) know about it.  If someone decides they don’t want updates anymore, they quietly unsubscribe, and the channel won't send any notifications.

### Low Level Design

<figure><img src="/files/6cB3IB7rnxV2xV2lQHWu" alt=""><figcaption><p>Observer Design Pattern</p></figcaption></figure>

Let's discuss the above diagram , considering Youtube Channel and Subscribers as an example.

### Requirement

* Subscriber should be able to subscribe to Channel
* Whenever Channel uploads a video, channel should notify its subscribers

### Implementation

We will implement above diagram considering

Observable = Channel

Observer = Subscriber

#### The interfaces

```java
public interface Observable { // channel

	void addSubscriber(Subscriber subscriber);

	void removeSubscriber(Subscriber subscriber);

	void notifySubscribers();

	void upload(Video video);
}
```

```java
public interface Subscriber {
	void update();
}
```

#### Concrete Classes

Let's first create a Video class which will be uploaded by Channel.

```java
@Data
@AllArgsConstructor
public class Video {
	String name;
	String description;
}
```

{% hint style="info" %}
I have used **Lombok** library annotations @Data for boilerplate getter/setter methods and @AllArgsConstructor for parametrized constructor.&#x20;
{% endhint %}

Now let's create concrete obervable and observer classes. In our example, we will create ChannelObservable and ChannelSubscriber concrete classes.

```java
public class ChannelObservable implements Observable {
	//Has-A
	private List<ChannelSubscriber> subs = new ArrayList<>();

	private Video video;

	@Override
	public void addSubscriber(ChannelSubscriber subscriber) {
		subs.add(subscriber);
	}

	@Override
	public void removeSubscriber(ChannelSubscriber subscriber) {
		subs.remove(subscriber);
	}

	@Override
	public void notifySubscribers() {
		for (ChannelSubscriber sub : subs) {
			sub.update();
		}
	}

	@Override
	public void upload(Video video) {
		this.video = video;
		notifySubscribers();
	}

	public String getTitle() { // for state change
		return video.getName();
	}
}
```

```java
public class ChannelSubscriber implements Subscriber{
	private String subscriberName;

	//Has-A
	private ChannelObservable channel;

	public ChannelSubscriber(String name, ChannelObservable observable) {
		this.subscriberName = name;
		this.channel = observable;
	}

	@Override
	public void update() {
		System.out.println("Hey " + subscriberName + ", " +
			"Video Uploaded about " + this.channel.getTitle());
	}
}
```

Now it's time to create our app called Youtube :tada:

```java
public class Youtube {

	public static void main(String[] args) {
		ChannelObservable channel = new ChannelObservable();

		ChannelSubscriber s1 = new ChannelSubscriber("X", channel);
		ChannelSubscriber s2 = new ChannelSubscriber("Y", channel);

		channel.addSubscriber(s1);
		channel.addSubscriber(s2);

		channel.upload(new Video("Design Pattern in Java",
			"All about design patterns"));

		channel.removeSubscriber(s2);

		channel.upload(new Video("Java8 functional programming",
			"All about functional Programming"));
	}
}
```

#### Output

```
Hey X, Video Uploaded about Design Pattern in Java
Hey Y, Video Uploaded about Design Pattern in Java
Hey X, Video Uploaded about Java8 functional programming
```

Hope you have understood the pattern in a simple manner&#x20;
