REMEMBER JAVA

MINTE9
2REMEMBER




Last update:   23-07-2021

Observer Pattern

Observer Pattern is pretty much like newspaper subscriptions. We call the publisher the subject and the subscribers the observers. The Publisher notifies subscribers automatically when any state changes.
 RunCode
/**
 * Basic Skeleton
 */
class ExchangeRates {
    
    public static void main(String[] args) {

        CentralBank feed = new CentralBank();
        Bank ING = new Bank("ING");
        Bank BNP = new Bank("BNP");
        
        feed.subscribe(ING);
        feed.subscribe(BNP);

        feed.setRates();
        feed.notifyObservers(); // Look Here
    }
}

class CentralBank implements Subject {
    public void setRates() {}
    public void subscribe(Observer o) {}
    public void unsubscribe(Observer o) {}
    public void notifyObservers() {
        System.out.println("Observers were notified!");
    }
}

class Bank implements Observer {
    public Bank(String name) {}
    public void update() {} // Look Here
}

interface Observer {
    public void update();
}

interface Subject {
    public void subscribe(Observer o);
    public void unsubscribe(Observer o);
    public void notifyObservers();
}
... 34 lines
˄˄˄
 RunCode
import java.util.ArrayList;

/**
 * Observer subscribe/unsubscribe themselves
 * Subject Instance variable (feed) is passed as parameter
 */
public class ExchangeRates {
    
    public static void main(String[] args) {

        CentralBank feed = new CentralBank();

        Bank ING = new Bank("ING", feed);
        Bank BNP = new Bank("BNP", feed);
        
        feed.updateRates("Monday");
        
        BNP.unsubscribe();
        feed.updateRates("Tuesday");
    }
}

class CentralBank implements Subject { // The Publisher

    ArrayList<Observer> observers = new ArrayList<>();
    String rate;

    public void updateRates(String day) {

        if (day == "Monday") { rate = "EURUSD:1.2222"; }
        if (day == "Tuesday") { rate = "EURUSD:1.4444"; }
        System.out.println("CentralBank: [Rates updated] ---");
        
        notifyObservers();
    }

    public void subscribe(Observer o) {
        observers.add(o);
    }

    public void unsubscribe(Observer o) {
        observers.remove(o);
    }

    public void notifyObservers() {
        for(Observer o : observers) { // Look Here
            o.update(rate); 
            System.out.println("CentralBank: [" + o.getName() + " notified]");
        }
    }
}

class Bank implements Observer {

    private String name;
    private Subject feed;
    private String rate;
    
    public Bank(String n, Subject f) {
        name = n;
        feed = f;
        feed.subscribe(this);
    }

    @Override
    public String getName() { return name; }

    public void update(String r) { // Look Here
        rate = r;
        System.out.print("    " + name + ": " + rate + " / ");
    }

    public void unsubscribe() {
        feed.unsubscribe(this);
        System.out.println(name + ": I've [unsubscribed] from Rates Feed!");
    }
}

interface Observer {
    public void update(String rate);
    public String getName();
}

interface Subject {
    public void subscribe(Observer o);
    public void unsubscribe(Observer o);
    public void notifyObservers();
}

/*

CentralBank: [Rates updated] ---
    ING: EURUSD:1.2222 EURGBP:0.3333  / CentralBank: [ING notified]
    BNP: EURUSD:1.2222 EURGBP:0.3333  / CentralBank: [BNP notified]
BNP: I've [unsubscribed] from Rates Feed!
CentralBank: [Rates updated] ---
    ING: EURUSD:1.4444 EURGBP:0.6666  / CentralBank: [ING notified]

*/
... 74 lines
˄˄˄
Observer v3.0 Basic Skeleton Observer v3.2 Subscribe and notify observers Observer v3.4 Update rates / Observer unsubscribe
Questions    
No. 30   Strategy
CARD
29
63

        A B C D E F
🔔
1/5