I just wanted to go over Observable pattern, and since I am used to with Java the most, I will write code in Java. But, of course, if you know the concept behind this pattern, you can definitely use it in any language you like.

Say there is a School, and this school has many classes they run.From the student point of view, he/she is supposed to get the message sent by all the courses he/she is involved. So, say Tom is taking cs101, eng101 and phy101, he should be getting messages from these courses. If cs101 is not taken by Sam, he shouldn’t be getting any messages from him. In this case, the courses is Observable, where each student, observer, subscribes the change happening to specific course.

But, before I start with my own example, here is Observable and Observer in rough way. Java has it for you, you can use theirs, but if you want to create your own, you can do so.

import java.util.Observer;

public class Observable {

//adds the observer
public void addObserver(Observer observer);

public void deleteObserver(Observer observer);

public void notifyObservers(Object o);

public interface Observer {

public void update(Observable o, Object arg);

I am going to design School, Students and Activities, using Javas implementation:

In the Object below, by implementing Observer, we are saying it to observe the change that is going to be received in the overridden method update. Later, we are going to create a Course, and we will design Course in such a way that it will be able to send message to Student.

import java.util.Observable;
import java.util.Observer;

public class Student implements Observer {

String name;

public Student(String name) {
this.name = name;

public void update(Observable observable, Object o) {
name + ": Notification recieved: " + (String) o

In Course, by extending Observable, we say that this is where the change of event/data takes place.

  1. addObserver(student) tells the Observable object to know that Student wants to observe the change. deleteObserver(student) removes it.
  2. In method, sendMessage, we call setChanged() to tell Observable that data is changed, and now, notify the observers using notifyObservers(s) with the new data.
import java.util.Observable;

public class Course extends Observable {

public Course() {


public void sendMessage(String s){
setChanged(); //let Observable know the change
notifyObservers(s); //notify observers attached to it

public void addStudent(Student student) {
addObserver(student); //add student as observer

public void removeStudent(Student student) {
deleteObserver(student);//remove the student from observer

Now the main activity, where we show how this works.

public class School {

public static void main(String[] args) {

Student tom = new Student("tom");
Student sam = new Student("sam");
Student leslie = new Student("leslie");

Course cs101 = new Course();

Course eng101 = new Course();

Course phy101 = new Course();

cs101.sendMessage("No class tomorrow");
eng101.sendMessage("5 essays due tonight");
phy101.sendMessage("Quizes tomorrow!");



leslie: Notification recieved: No class tomorrow
tom: Notification recieved: No class tomorrow
sam: Notification recieved: 5 essays due tonight
tom: Notification recieved: 5 essays due tonight
tom: Notification recieved: Quizes tomorrow!
leslie: Notification recieved: Quizes tomorrow!
sam: Notification recieved: Quizes tomorrow!