Observer Pattern

Define a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and update automatically.
Also known as Publish-Subscribe.


You may have one observable(Publish), but many observers(Subscribe).
In the following example,
observable (or publish) -- XiaoMing
observers (or subscribe) -- observer1, observer2, observer3, ...
The pattern consists of 6 parts.

1.interface Observable: operations about observers.
addObserver(Observer observer),
deleteObserver(Observer observer),
notifyObservers(String context);

2.interface Observer: what should observer do?
update(String context);

3.interface ListenedContent: the content of observer wants to listen.
haveDinner();
haveFun();

4.class XiaoMing implements Observable, ListenedContent;

5.class Observer1 implement Observer;

6.main test.


//1. operations about observer.
public interface Observable {
    public void addObserver(Observer observer);
    public void deleteObserver(Observer observer);
    public void notifyObservers(String content);
}
//2.  the action observer will do, when getting info(context) from the publish.
public interface Observer{
     public void update(String context);
}
// 3. Define 2 activities that observers want to know. 
//When doing one of them or both, they will notify the observers.
public interface ListenedContent {
    public void haveDinner();
    public void hanveFun();
}
//4. publish class object.
public class XiaoMing implements Observable, ListenedContent {
    // Observable part.
    private ArrayList list = new ArrayList();
    public void addObserver(Observer observer){
        list.add(observer);
    }
    public deleteObserver(Observer observer){
        list.remove(observer);
    }
    public notifyObservers(String context){
        for(Observer observer : list){
             observer.update(context);
        }
    }

    //ListenedContent part.
    public void haveDinner(){
        System.out.println("XiaoMing is having dinner.");
        notifyObservers("have dinner");
    }
    public void haveFun(){
        System.out.println("XiaoMing is having fun.");
        notifyObservers("have fun");
    }

}
// observer1, you can also have observer2, observer3, ...
public class Observer1 implments Observer {
    public void update(String context){
        System.out.println("Start to get info from XiaoMing.");
        getContent(context);
   }
    private void getContent(String context){    // private
        System.out.println("XiaoMing is doing: " + context);
    }
}
// Test
public class Test {
    public static void main(String[] args){
        XiaoMing xiaoming = new XiaoMing();
        Observer observer1 = new Observer1();
        xiaoming.addObserver(observer1);    // add observer.
        xiaoming.haveDinner();    // observer1 will be notified.
        xiaoming.haveFun();       // observer1 will be notified.
        // At last, what xiaoming does, observers will be notified.
    }
} 

你可能感兴趣的:(Observer Pattern)