Java8 features implement the observer pattern of common design patterns

Observer pattern: When certain events occur (such as state transitions), if an object (usually we call it a subject) needs to automatically notify multiple other objects (called observers)


Example: Implementing a custom notification system. Several newspaper organizations, such as The New York Times, The Guardian, and Le Monde, subscribe to News Center, and they want to be notified when news they receive contains keywords that interest them.

1. Java traditional implementation:

step 1

Provide observers with an interface, so that observers can customize and implement their own subscription strategy (keywords they are interested in).

Observer.java

interface Observer {
    void notify(String tweet);
}

Step 2

Observers each implement the interface

NYTimes.java

Guardian.java

LeMonde.java

class NYTimes implements Observer{
        public void notify(String tweet) {
            if(tweet != null && tweet.contains("money")){
                System.out.println("Breaking news in NY! " + tweet);
        }
    }
}
class Guardian implements Observer{
        public void notify(String tweet) {
            if(tweet != null && tweet.contains("queen")){
                System.out.println("Yet another news in London... " + tweet);
        }
    }
}
class LeMonde implements Observer{
        public void notify(String tweet) {
            if(tweet != null && tweet.contains("wine")){
                System.out.println("Today cheese, wine and news! " + tweet);
           }
       }
}

Step 3

Define an interface Subject of a news center, there are two methods, the registerObserver method registers a new observer, and the notifyObservers method notifies its observers of the arrival of a news

Subject.java

interface Subject{
        void registerObserver(Observer o);
        void notifyObservers(String tweet);
}

Step 4

Implement the interface Subject of the news center and define the news center class.

Feed.java

class Feed implements Subject{
        private final List<Observer> observers = new ArrayList<>();
        public void registerObserver(Observer o) {
            this.observers.add(o);
    }
        public void notifyObservers(String tweet) {
            observers.forEach(o -> o.notify(tweet));
    }
}

Step 5

It notifies when a piece of news arrives.

Feed f = new Feed();
f.registerObserver(new NYTimes());
f.registerObserver(new Guardian());
f.registerObserver(new LeMonde());
f.notifyObservers("The queen said her favourite book is Java 8 in Action!");

2、Java8实现:

使用Lambda表达式后,你无需显式地实例化三个观察者对象,直接传递Lambda表达式表示需要执行的行为即可。
f.registerObserver((String tweet) -> {
        if(tweet != null && tweet.contains("money")){
            System.out.println("Breaking news in NY! " + tweet);
    }
});
f.registerObserver((String tweet) -> {
        if(tweet != null && tweet.contains("queen")){
            System.out.println("Yet another news in London... " + tweet);
    }
});



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651177&siteId=291194637