Design Patterns: Observer Pattern

I am working my way through Head First Design Patterns and I will be sharing my notes on each topic. Design patterns are an important object oriented programming concept as they help software engineers have an arsenal of solutions to problems that show up often. I really want to hone in on and refine my craft as a software engineer so it makes sense to me to understand these concepts. I will share my journey along the way! I may even come up with an Anki deck of this book.

I owe a big thanks to Christopher Okhravi. Check out his YouTube series on the book here.

I will put keep all my notes on this notion page here so feel free to use that as a reference. The rest of this blog post will simply be a copy paste of the information there. If you feel that my notes are lacking or incorrect in some way, please let me know! I am open to criticism.

My TL;DR Definition:

An object's state is broadcast to it's subscribers if a change occurs

Slightly Longer Definition:

There is a relationship between two kinds of objects: a subject and an observer. There is only one subject object and it will update the observers when a change occurs to its internal state. The observers are subscribed to the subject and concerned with the subject's state for any number of reasons. The observers can freely leave or join the relationship.

Earthquake Notifier Example:

Suppose there is a town that frequently has earthquakes. The citizens can get hurt if they are not safe place when an earthquake occurs. The town has installed some monitoring instruments and a seismograph lab. The scientists have a system that can predict when an earthquake is going to occur an hour in advance. (This is just an example, I don't know if this is actually possible?) The last thing that is needed is a system that can broadcast the warning to the townspeople via their smart device. This way, they can plan accordingly and head to safety.

Pseudocode:

(fake C#/Java-ish code)

public interface Subject
{
    public void addObserver(Observer observer);
    public void removeObserver(Observer observer);
    public void notify();
}

public interface Observer
{
    public void update(bool earthquakeStatus);
}

public class EarthquakeLab implements Subject
{
    private ArrayList observers;
    private bool earthquakeStatus;

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

    public void addObserver(Observer observer)
    {
        observers.add(observer);
    }

    public void removeObserver(Observer observer)
    {
        observers.remove(observer);
    }

    public void notify()
    {
        foreach(ob in observers)
        {
            ob.update(earthquakeStatus)
        }
    }

    public void measurementChanged()
    {
        notify()
    }

    public void getEarthquakeStatus(bool earthquakeStatus)
    {
        this.earthquakeStatus = earthquakeStatus;
    }
}

public class PhoneDisplay implements Observer
{
    private bool earthquakeStatus;
    private Subject earthquakeLab;

    public PhoneDisplay(Subject earthquakeLab)
    {
        this.earthquakeLab = earthquakeLab;
        earthquakeLab.addObserver(this);
    }

    public void update(bool earthquakeStatus)
    {
        this.earthquakeStatus = earthquakeStatus;
        display();
    }

    public void display()
    {
        //notification shows up on phone screen
    }
}

No Comments Yet