[Design Pattern] Verhaltensmuster: Observer


Thomas Darimont

Premium-User
Hallo,

dieser Beitrag erklärt das Verhaltensmuster: Observer
Java:
package com.dacos.training.java.patterns.behavioral;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
public class ObserverPatternExample {
 
    public static void main(String[] args) {
        StockBroker trader1 = new StockBroker("Bob");
        StockBroker trader2 = new StockBroker("Joe");
        StockBroker trader3 = new StockBroker("Lee");
 
        StockExchange se = new StockExchange();
        se.add(trader1);
        se.add(trader2);
        se.add(trader3);
 
        se.trade();
    }
 
    static class StockExchange {
        List<Share> shares = Arrays.asList(new Share("IBM", 499), new Share(
                "ORCL", 399), new Share("GOOG", 99));
        List<StockBroker> traders = new ArrayList<StockBroker>();
 
        public void add(StockBroker trader) {
            traders.add(trader);
        }
 
        public void trade() {
            Executors.newSingleThreadExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    registerTradersForShares();
 
                    while (true) {
 
                        for (Share s : shares) {
                            if (Math.random() > 0.9) {
                                s.setPrice(s.getPrice() + 1.1);
                                s.notifyObservers(System.currentTimeMillis());
                            }
                        }
 
                        try {
                            TimeUnit.MILLISECONDS.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
 
            });
 
        }
 
        protected void registerTradersForShares() {
            for (Share share : shares) {
                for (StockBroker trader : traders) {
                    share.addObserver(trader);
                }
            }
        }
 
    }
 
    static class StockBroker implements Observer {
 
        private final String name;
 
        public StockBroker(String name) {
            this.name = name;
        }
 
        @Override
        public void update(Observable o, Object arg) {
            Share s = (Share) o;
            System.out.println(this
                    + String.format(" Observed update of share %s value %s in Transaction: %s",
                            s.getName(), s.getPrice(), arg));
        }
 
        @Override
        public String toString() {
            return "StockBroker [name=" + name + "]";
        }
    }
 
    static class Share extends Observable {
        private String name;
        private double price;
 
        public Share(String name, double price) {
            this.name = name;
            this.price = price;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        public double getPrice() {
            return price;
        }
 
        public void setPrice(double price) {
            this.price = price;
            setChanged();
        }
 
        @Override
        public String toString() {
            return "Share [name=" + name + ", price=" + price + "]";
        }
    }
 
}
Ausgabe:
Code:
StockBroker [name=Lee] Observed update of share IBM value 500.1 in Transaction: 1325520567679
StockBroker [name=Joe] Observed update of share IBM value 500.1 in Transaction: 1325520567679
StockBroker [name=Bob] Observed update of share IBM value 500.1 in Transaction: 1325520567679
StockBroker [name=Lee] Observed update of share IBM value 501.20000000000005 in Transaction: 1325520570254
StockBroker [name=Joe] Observed update of share IBM value 501.20000000000005 in Transaction: 1325520570254
StockBroker [name=Bob] Observed update of share IBM value 501.20000000000005 in Transaction: 1325520570254
StockBroker [name=Lee] Observed update of share GOOG value 100.1 in Transaction: 1325520570754
StockBroker [name=Joe] Observed update of share GOOG value 100.1 in Transaction: 1325520570754
StockBroker [name=Bob] Observed update of share GOOG value 100.1 in Transaction: 1325520570754
StockBroker [name=Lee] Observed update of share GOOG value 101.19999999999999 in Transaction: 1325520573285
StockBroker [name=Joe] Observed update of share GOOG value 101.19999999999999 in Transaction: 1325520573285
StockBroker [name=Bob] Observed update of share GOOG value 101.19999999999999 in Transaction: 1325520573285
StockBroker [name=Lee] Observed update of share IBM value 502.30000000000007 in Transaction: 1325520576296
StockBroker [name=Joe] Observed update of share IBM value 502.30000000000007 in Transaction: 1325520576296
StockBroker [name=Bob] Observed update of share IBM value 502.30000000000007 in Transaction: 1325520576296
StockBroker [name=Lee] Observed update of share ORCL value 400.1 in Transaction: 1325520576296
StockBroker [name=Joe] Observed update of share ORCL value 400.1 in Transaction: 1325520576296
StockBroker [name=Bob] Observed update of share ORCL value 400.1 in Transaction: 1325520576296
StockBroker [name=Lee] Observed update of share IBM value 503.4000000000001 in Transaction: 1325520576801
StockBroker [name=Joe] Observed update of share IBM value 503.4000000000001 in Transaction: 1325520576801
StockBroker [name=Bob] Observed update of share IBM value 503.4000000000001 in Transaction: 1325520576801
StockBroker [name=Lee] Observed update of share ORCL value 401.20000000000005 in Transaction: 1325520580347
StockBroker [name=Joe] Observed update of share ORCL value 401.20000000000005 in Transaction: 1325520580347
StockBroker [name=Bob] Observed update of share ORCL value 401.20000000000005 in Transaction: 1325520580347
StockBroker [name=Lee] Observed update of share GOOG value 102.29999999999998 in Transaction: 1325520580847
StockBroker [name=Joe] Observed update of share GOOG value 102.29999999999998 in Transaction: 1325520580847
StockBroker [name=Bob] Observed update of share GOOG value 102.29999999999998 in Transaction: 1325520580847
StockBroker [name=Lee] Observed update of share IBM value 504.5000000000001 in Transaction: 1325520587494
StockBroker [name=Joe] Observed update of share IBM value 504.5000000000001 in Transaction: 1325520587494
StockBroker [name=Bob] Observed update of share IBM value 504.5000000000001 in Transaction: 1325520587494
Gruß Tom