PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   10-10-2021

Observer

Observer Pattern is pretty much like newspaper subscriptions. The Publisher notifies subscribers automatically when any state changes.
 
import java.util.ArrayList;

class App {
    
    public static void main(String[] args) {

        Publisher feed = new Publisher();
        Bank ING = new Bank();
        Bank BNP = new Bank();

        feed.subscribe(ING);
        feed.subscribe(BNP);
        feed.setRates();
        feed.notifyObservers(); // Look Here
    }
}

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

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

class Publisher implements Subject {

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

    public void setRates() { rate = "EURUSD:1.2222"; }

    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); 
        }
    }
}

class Bank implements Observer {
    public void update(String rate) { // Look Here
        System.out.println(this + ": " + rate);
    }
}

/**
    Bank@4d7e1886: EURUSD:1.2222
    Bank@3cd1a2f1: EURUSD:1.2222
**/
... 35 lines
 
Observers can subscribe/unsubscribe themselves v3.4
 
import java.util.ArrayList;

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

        Publisher feed = new Publisher();

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

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();
}

class Publisher implements Subject {

    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); // client subscribe 
    }

    @Override public String getName() { 
        return name; 
    }
    @Override 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!");
    }
}

/*

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]

*/
... 69 lines
 
A listener is an implementation of observer pattern.
 
import java.util.ArrayList;

class App {
    
    public static void main(String[] args) {

        Service service = new Service();
        service.addListener(new MessageListener() { // observer

            @Override public void onMessageReceived(String message) {
                System.out.println("Client received message: " + message);
            }

            @Override public void onDisconnect() {
                System.out.println("Client disconnected");
            }
        });
        service.sendMessage("order_id 120");
        service.notifyListeners();

        service.sendMessage("disconnect");
        service.notifyListeners();
    }
}

interface MessageListener {
    public void onMessageReceived(String message);
    public void onDisconnect();
}

interface Subject {
    public void addListener(MessageListener o);
    public void notifyListeners();
}

class Service implements Subject {

    ArrayList<MessageListener> observers = new ArrayList<>();
    private String message;

    public void addListener(MessageListener o) { observers.add(o); }
    public void removeListener(MessageListener o) { observers.remove(o); }

    public void sendMessage(String message) {
        this.message = message;
    }

    public void notifyListeners() {
        
        for(MessageListener o : observers) { // Look Here

            if (message.contains("order")) { 
                o.onMessageReceived("new order");
            }
            
            if (message.contains("disconnect")) {
                o.onDisconnect();
            }
        }
    }
}

/**
    Client received message: new order
    Client disconnected
**/
... 43 lines
 

Questions    
Strategy

        A B C D E F
🔔
1/5