1、观察者模式:定义了对象间的一对多依赖,当一个对象改变状态时,它的所有依赖者能自动得到通知并更新。(出版
     者+订阅者=观察者模式)
2、java.util.Observer和java.util.Observable提供内置的观察者模式支持,可以使用推和拉的方 式传送数据,注
     意:不要依赖它的通知次序。其实现步骤如下:
    1>.将对象变为观察者:
         <1>.实现观察者接口(java.util.Observer)
         <2>.调用addObserver()方法即将对象变为观察者
         <3>.如想退出观察者,调用deleteObserver()即可
    2>.主题发送通知:
         <1>.调用setChanged()方法,标记状态已改变
         <2>.调用notifyObservers():
                notifyObservers()或notifyObservers(Object o)(其中o表示要传送的数据对象)
    3>.观察者接受通知:
         update(Observable o,Object o)
3、实现一个完整的观察者模式示例(推模式):不使用java的内置API
      1>.设计UML图:
      2>.Coding
           <1>.主题部分
//Subject interface
public interface Subject{

    public void registerObserver(Observer observer);
    public void removeObserver(Observer observer);
    public void notifyObservers();

}
//MapSeries:implements Subject interface
import java.util.ArrayList;

public class MapSeries implements Subject{

    private ArrayList<Observer> observers;
    private String name;
    private String author;
    private double price;

    public MapSeries(){
         observers = new ArrayList<Observer>();
    }

    public void registerObserver(Observer observer){
         observers.add(observer);
    }

    public void removeObserver(Observer observer){
         int i = observers.indexOf(observer);
         if(i > -1){
             observers.remove(i);
         }
    }

    public void notifyObservers(){
         for(int i = 0; i < observers.size(); i++){
              Observer observer = observers.get(i);
              observer.update(name,author,price);
         }
    }  

    public void autoNotify(){
         notifyObservers();
    }

    public void addBook(String name, String author, double price){
         this.name = name;
         this.author = author;
         this.price = price;
         autoNotify();
    }

}
             <2>.观察者部分
//Observer interface
public interface Observer{

    public void update(String name,String author,double price);

}
//Display interface
public interface Display{

    public void display();

}
//display name and author
public class NameAndAuthor implements Observer,Display{

    private String name;
    private String author;
    private Subject subject;

    public NameAndAuthor(Subject subject){
         this.subject = subject;
         subject.registerObserver(this);
    }

    public void update(String name,String author,double price){
         this.name = name;
         this.author = author;
         display();
    }

    public void display(){
         System.out.println("Name: " + name + "\n"  + "Author: " + author);
    }

}
//display name and price
public class NameAndPrice implements Observer,Display{

    private String name;
    private double price;
    private Subject subject;

    public NameAndPrice(Subject subject){
         this.subject = subject;
         subject.registerObserver(this);
    }

    public void update(String name,String author,double price){
         this.name = name; 
         this.price = price;
         display();
    }

    public void display(){
         System.out.println("Name: " + name + "\n" + "Price: " + String.valueOf(price));
    }

}
//display name author price
public class NameAuthorPrice implements Observer,Display{

    private String name;
    private String author;
    private double price;
    private Subject subject;

    public NameAuthorPrice(Subject subject){
         this.subject = subject;
         subject.registerObserver(this);
    }

    public void update(String name,String author,double price){
         this.name = name; 
         this.author =author;
         this.price = price;
         display();
    }

    public void display(){
         System.out.println("Name: " + name + "\n"  
                     + "Author: " + author + "\n"  
                     + "Price: " + String.valueOf(price) + "\n");
    }

}
             <3>.测试部分
//test this application program
public class TestObserver{

    public static void main(String[] args){
  
         MapSeries map = new MapSeries();

         NameAndAuthor na = new NameAndAuthor(map);
         NameAndPrice np = new NameAndPrice(map);
         NameAuthorPrice nap = new NameAuthorPrice(map); 

         map.addBook("Head First Design Pattern","BuFuBuXing",98.00); 
         map.addBook("Head First Servlet&Jsp","Wenlong.Meng",99.99);
         map.addBook("Head First EJB","Dragon",87.00);

    }

}
             <4>.测试结果部分
4、运用java内置观察者模式支持示例:拉模式
      1、可观察者部分
//MapSeries:extend Observable class
import java.util.Observable;

public class MapSeries extends Observable{

    private String name;
    private String author;
    private double price;

    public MapSeries(){}

    public void autoNotify(){
         setChanged();
         notifyObservers();//"拉"模式:后台调用notifyObservers(null) 
    }

    public void addBook(String name, String author, double price){
         this.name = name;
         this.author = author;
         this.price = price;
         autoNotify();
    }

    public String getName(){
         return name;
    }

    public String getAuthor(){
         return author;
    }

    public double getPrice(){
         return price;
    }

}
      2>.观察者部分
//display name and author
import java.util.Observable;
import java.util.Observer;

public class NameAndAuthor implements Observer,Display{

    Observable observable;
    private String name;
    private String author;

    public NameAndAuthor(Observable observable){
         this.observable = observable;
         observable.addObservser(this);
    }

    public void update(Observable obs,Object org){

         if(obs instanceof MapSeries){
              MapSeries map = (MapSeries)obs; 
              this.name = map.getName();
              this.author = map.getAuthor();
              display();
         }
  
    }

    public void display(){
         System.out.println("Name: " + name + "\n"  + "Author: " + author);
    }

}
//display name author price
import java.util.Observable;
import java.util.Observer;

public class NameAuthorPrice implements Observer,Display{

    private String name;
    private String author;
    private double price;
    private Observable observable;

    public NameAuthorPrice(Observable observable){
         this.observable = observable;
         observable.addObservser(this);
    }

    public void update(Observable observable, Object arg){

         if(obs instanceof MapSeries){
              MapSeries map = (MapSeries)obs;
              this.name = map.getName();
              this.author = map.getAuthor();
              this.price = map.getPrice();
              display(); 
         }

    }

    public void display(){
         System.out.println("Name: " + name + "\n"  
                     + "Author: " + author + "\n"  
                     + "Price: " + String.valueOf(price) + "\n");
    }

}
//display name and price
import java.util.Observable;
import java.util.Observer;
public class NameAndPrice implements Observer,Display{

    private String name;
    private double price;
    private Observable observable;

    public NameAndPrice(Observable observable){
         this.observable = observable;
         observable.addObservser(this);
    }

    public void update(String name,String author,double price){

         if(obs instanceof MapSeries){
              MapSeries map = (MapSeries)obs; 
              this.name = map.getName();
              this.price = map.getPrice();
              display(); 
         }

    }

    public void display(){
         System.out.println("Name: " + name + "\n" + "Price: " + String.valueOf(price));
    }

}
      3>.测试部分
//test this application program
public class TestObserver{

    public static void main(String[] args){
  
         MapSeries map = new MapSeries();

         NameAndAuthor na = new NameAndAuthor(map);
         NameAndPrice np = new NameAndPrice(map);
         NameAuthorPrice nap = new NameAuthorPrice(map);

         map.addBook("Head First Design Pattern","BuFuBuXing",98.00);
         map.addBook("Head First Servlet&Jsp","Wenlong.Meng",99.99);
         map.addBook("Head First EJB","Dragon",87.00);

    }

}
      4>.结果部分
5、运用java内置观察者模式支持示例:推模式
      1、可观察者部分
//MapSeries:extend Observable class
import java.util.ArrayList;
import java.util.Observable;

public class MapSeries extends Observable{

    private String name;
    private String author;
    private double price;

    public MapSeries(){}

    public void autoNotify(){
         ArrayList al = new ArrayList();
         al.add(name);
         al.add(author);
         al.add(price);
         setChanged();
         notifyObservers(al);
        //notifyObservers();// 
    }

    public void addBook(String name, String author, double price){
         this.name = name;
         this.author = author; 
         this.price = price;
         autoNotify();
    }

    /*public String getName(){ 
         return name;
    }

    public String getAuthor(){
         return author;
    }

    public double getPrice(){
         return price;
    }*/


}
      2>.观察者部分
//Display interface
public interface Display{

    public void display();

}
//display name and author
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class NameAndAuthor implements Observer,Display{

    Observable observable;
    private String name;
    private String author;

    public NameAndAuthor(Observable observable){
        this.observable = observable;
        observable.addObserver(this);
    }

    public void update(Observable obs,Object org){
  
         if(org != null){
              name = (String)(((ArrayList)org).get(0));
              author = (String)(((ArrayList)org).get(1));
              display();
         } 
   
         /*if(obs instanceof MapSeries){
         MapSeries map = (MapSeries)obs;
         this.name = map.getName();
         this.author = map.getAuthor();
         display();
         }*/

  
    }

    public void display(){
         System.out.println("Name: " + name + "\n"  + "Author: " + author + "\n");
    }

}
//display name author price
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class NameAuthorPrice implements Observer,Display{

    private String name;
    private String author;
    private double price;
    private Observable observable;

    public NameAuthorPrice(Observable observable){
         this.observable = observable;
         observable.addObserver(this);
    }

    public void update(Observable obs, Object org){
  
        if(org != null){
             name = (String)(((ArrayList)org).get(0));
             author = (String)(((ArrayList)org).get(1));
             price = ((Double)(((ArrayList)org).get(2))).doubleValue();
             display();
        }

        /*if(obs instanceof MapSeries){
             MapSeries map = (MapSeries)obs;
             this.name = map.getName();
             this.author = map.getAuthor();
             this.price = map.getPrice();
             display();
        }*/


    }

    public void display(){
         System.out.println("Name: " + name + "\n"  
                      + "Author: " + author + "\n"  
                      + "Price: " + String.valueOf(price) + "\n");
    }

}
//display name and price
import java.util.Observable;
import java.util.Observer;
import java.util.ArrayList;

public class NameAndPrice implements Observer,Display{

    private String name;
    private double price;
    private Observable observable;

    public NameAndPrice(Observable observable){ 
        this.observable = observable;
        observable.addObserver(this);
    }

    public void update(Observable obs,Object org){

        if(org != null){
             name = (String)(((ArrayList)org).get(0));
             price = ((Double)(((ArrayList)org).get(2))).doubleValue();
             display();
        }

        /*if(obs instanceof MapSeries){
             MapSeries map = (MapSeries)obs;
             this.name = map.getName();
             this.price = map.getPrice(); 
             display();
        }*/


    }

    public void display(){
        System.out.println("Name: " + name + "\n" + "Price: " + String.valueOf(price) + "\n");
    }

}
      3>.测试部分
//test this application program
public class TestObserver{

    public static void main(String[] args){
  
        MapSeries map = new MapSeries();

        NameAndAuthor na = new NameAndAuthor(map);
        NameAndPrice np = new NameAndPrice(map);
        NameAuthorPrice nap = new NameAuthorPrice(map);

        map.addBook("Head First Design Pattern","BuFuBuXing",98.00);
        map.addBook("Head First Servlet&Jsp","Wenlong.Meng",99.99);
        map.addBook("Head First EJB","Dragon",87.00);

    }

}
      4>.结果部分