Observer Pattern Example

package com.javagyan.examples;

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

class Stock extends Observable { // Subjects must extend Observable Interface
    private String stockName;

    private float stockPrice;

    private Long tradingVolume;

    public Stock(final String name, final float price, final Long tradingVolume) {
        this.stockName = name;
        this.stockPrice = price;
        this.tradingVolume = tradingVolume;
        System.out.println("Stock created: " + name + " at + price");
    }

    public String getName() {
        return this.stockName;
    }

    public float getPrice() {
        return this.stockPrice;
    }

    public Long getTradingVolume() {
        return this.tradingVolume;

    }

    public void setName(final String name) {
        this.stockName = name;
    }

    public void setPrice(final float price) {
        this.stockPrice = price;
        setChanged();
        notifyObservers(new Float(price));
    }

    public void setTradingVolume(final Long tradingVolume) {
        this.tradingVolume = tradingVolume;
        setChanged();
        notifyObservers(tradingVolume);
    }
}

/** 1st OBSERVER */
class TradingVolumeObserver implements Observer { // Observers must extend Observer Interface
    private Long tradingVolume;

    public TradingVolumeObserver() {
        this.tradingVolume = null;
        System.out.println("TradingVolumeObserver created: tradingVolume is " + this.tradingVolume);
    }

    public void update(final Observable obj, final Object arg) {
        if (arg instanceof Long) {
            this.tradingVolume = (Long) arg;
            System.out.println("TradingVolumeObserver: tradingVolume changed to " + this.tradingVolume);
        } else {
            System.out.println("TradingVolumeObserver: Some other property changed on subject!");
        }
    }
}

/** 2nd OBSERVER */
class PriceObserver implements Observer {
    private float price;

    public PriceObserver() {
        this.price = 0;
        System.out.println("PriceObserver created: Price is " + this.price);
    }

    public void update(final Observable obj, final Object arg) {
        if (arg instanceof Float) {
            this.price = ((Float) arg).floatValue();
            System.out.println("PriceObserver: Price changed to " + this.price);
        } else {
            System.out.println("PriceObserver: Some other property changed on subject!");
        }
    }
}

/** Lets see this in action now */
public class ObserverPatternExample {
    public static void main(final String args[]) {

        // Create the Subject and Observers.
        Stock s = new Stock("TATA MOTORS", 1000.50f, 10000L);
        TradingVolumeObserver tradingVolumeObserver = new TradingVolumeObserver();
        PriceObserver priceObserver = new PriceObserver();

        // Add those Observers!
        s.addObserver(tradingVolumeObserver);
        s.addObserver(priceObserver);

        // Make changes to the Subject.
        s.setTradingVolume(20000L);
        s.setPrice(1010f);
        s.setTradingVolume(40000L);
        s.setPrice(1050f);
    }
}
=================================================
OUTPUT:
Stock created: TATA MOTORS at + price
TradingVolumeObserver created: tradingVolume is null
PriceObserver created: Price is 0.0
PriceObserver: Some other property changed on subject!
TradingVolumeObserver: tradingVolume changed to 20000
PriceObserver: Price changed to 1010.0
TradingVolumeObserver: Some other property changed on subject!
PriceObserver: Some other property changed on subject!
TradingVolumeObserver: tradingVolume changed to 40000
PriceObserver: Price changed to 1050.0
TradingVolumeObserver: Some other property changed on subject!
=================================================
Comments