Observer Pattern in Java

Observer Pattern is a Behavioral Pattern in which, an Object (we call it Subject) contains a list of dependency Objects (we call them Observers). Observers will be notified and updated automatically whenever Subject changes its state. Today we’re gonna look at how Observer Pattern in Java works.

I. OVERVIEW

Observer Pattern defines one-to-many relationship between Subject and Observers:
– The Subject maintains a list of Observers and has methods for adding, removing and notifying its Observers.
– When Subject changes its state, all Observers are notified and updated automatically.
– The Subject only know list of Observers and doesn’t care how they implement.
The image below shows how they interact with each other.

II. PRACTICE
1. Project Overview

To build an example for Observer Pattern, we will create some interfaces and their implementations.

Our goal is making a FanPage, and Fans who followed it will be notified automatically whenever FanPage create its schedule. After that, Fans wil update their own schedules depending on FanPage schedule. Fan also has the ability to unsubscribe FanPage.

It’s a little bit difficult. So we should look at an upgraded diagram of our Observer Pattern:

2. Step by Step

2.1- Create Observer interface:

package com.javasampleapproach.observerpattern.pattern;

public interface Observer {
	
	public void update(String schedule);
}

2.2- Create Subject interface:

package com.javasampleapproach.observerpattern.pattern;

public interface Subject {
	
	public void addObserver(Observer o);
	public void removeObserver(Observer o);
	public void notifyObserver();
}

2.3- Create implementation of Subject interface – FanPage class:

package com.javasampleapproach.observerpattern.pattern;

import java.util.ArrayList;

public class FanPage implements Subject {

	private ArrayList observers;
	private String schedule;

	public FanPage() {
		observers = new ArrayList();
	}

	@Override
	public void addObserver(Observer o) {
		observers.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		observers.remove(observers.indexOf(o));
	}

	@Override
	public void notifyObserver() {
		for (Observer o : observers) {
			o.update(schedule);
		}
	}

	public String getSchedule() {
		return schedule;
	}

	public void setSchedule(String schedule) {
		this.schedule = schedule;
		notifyObserver();
	}
}

2.4- Create implementation of Observer interface – Fan class:

package com.javasampleapproach.observerpattern.pattern;

public class Fan implements Observer {

	private String schedule;
	private int fanId;

	public static int observerId = 0;
	private Subject fanPage = null;

	public Fan(Subject fanPage) {
		this.fanPage = fanPage;
		this.fanId = ++observerId;
		fanPage.addObserver(this);
	}

	@Override
	public void update(String schedule) {
		this.schedule = schedule;
		setOwnSchedule();
	}

	private void setOwnSchedule() {
		System.out.println("Fan with Id " + this.fanId + " sets Schedule: " + this.schedule);
	}

	public void unsubscribe() {
		this.fanPage.removeObserver(this);
	}
}

2.5- Create Test Class:

package com.javasampleapproach.observerpattern;

import com.javasampleapproach.observerpattern.pattern.Fan;
import com.javasampleapproach.observerpattern.pattern.FanPage;

public class MainApp {

	public static void main(String[] args) {
		
		FanPage fanPage = new FanPage();
		
		Fan fan1 = new Fan(fanPage);
		Fan fan2 = new Fan(fanPage);
		
		System.out.println("FanPage sets their new schedule");
		fanPage.setSchedule("End of February");

		fanPage.removeObserver(fan1);
		
		System.out.println("FanPage sets their new schedule");
		fanPage.setSchedule("Beginning of March");
		
		fan2.unsubscribe();
		
		System.out.println("FanPage sets their new schedule");
		fanPage.setSchedule("End of March");
	}
}

2.6- Run the code, the console window shows:

FanPage sets their new schedule
Fan with Id 1 sets Schedule: End of February
Fan with Id 2 sets Schedule: End of February
FanPage sets their new schedule
Fan with Id 2 sets Schedule: Beginning of March
FanPage sets their new schedule
III. SOURCE CODE

ObserverPattern

Leave a Reply

Your email address will not be published. Required fields are marked *