观察者模式

因最近在研究PureMVC框架,而该框架的消息通知也就是采用了观察者模式。所以又重新看了一下该设计模式:

首先上源码:

 

 

===observer(观察者)参与者

    public interface Observer {
 public abstract void update(NumberGenerator generator);
}

 

 

====subject(目标)参与者

public abstract class NumberGenerator {

 private Vector observers = new Vector(); //存储Observer
 public void addServer(Observer observer){ //新增Observer
  observers.add(observer);
 }
 
 public void deleteObserver(Observer observer){ //删除Observer
  observers.remove(observer);
 }
 
 public void notifyObservers(){ //通知Observer
  Iterator it = observers.iterator();
  while(it.hasNext()){
   Observer o = (Observer)(it.next());
   o.update(this);
  }
 }
 
 public abstract int getNumber(); //取得数值
 public abstract void execute();  //产生数值
}

 

 

 

==ConcreteSubject(具体目标)参与者

public class RandomNumberGenerator extends NumberGenerator{
 private Random random = new Random();
 private int number;
 
 @Override
 public void execute() {
  for(int i = 0; i < 20; i++){
   number = random.nextInt(50);
   notifyObservers();
  }
 }

 @Override
 public int getNumber() {
  return number;
 }

}

 

 

====ConcreteObserver(具体观察者)参与者

public class DigitObserver implements Observer{

 @Override
 public void update(NumberGenerator generator) {
  System.out.println("DigitObserver: " + generator.getNumber());
  try{
   Thread.sleep(100);
  }catch(InterruptedException e){
   e.printStackTrace();
  }
 }

}

 

 

====ConcreteObserver(具体观察者)参与者

public class GraphObserver implements Observer{

 @Override
 public void update(NumberGenerator generator) {
  System.out.println("GraphObserver: ");
  int count = generator.getNumber();
  for(int i = 0; i < count; i++){
   System.out.print("*");
  }
  System.out.println("");
  try{
   Thread.sleep(100);
  }catch(InterruptedException e){
   e.printStackTrace();
  }
 }

}

 

 

public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  NumberGenerator generator = new RandomNumberGenerator();
  Observer observer1 = new DigitObserver();
  Observer observer2 = new GraphObserver();
  generator.addServer(observer1);
  generator.addServer(observer2);
  generator.execute();
 }

}

 

《设计模式: java语言中的应用》对以上代码的解析:

 

    观察者模式_第1张图片观察者模式_第2张图片

 

 

 

 

 实现java库中的观察者模式:

public class Watched extends Observable {

    private String data = "";

   

    public String retrieveData(){

       return data;

    }

 

    public void chageData(String data){

       if(!this.data.equals(data)){

           this.data = data;

           setChanged();

       }

       notifyObservers();

    }

}

====================================================================

 

import java.util.Observable;

import java.util.Observer;

 

public class Watcher implements Observer {

 

    public Watcher(Watched w){

       w.addObserver(this);

    }

   

    @Override

    public void update(Observable o, Object arg) {

       System.out.println("Data has been changed to: " + ((Watched)o).retrieveData() + "");

    }

}

===================================================================

 

import java.util.Observer;

 

public class Tester {

    static private Watched watched;

    static private Observer watcher;

   

    public static void main(String[] args) {

       watched = new Watched();

       watcher = new Watcher(watched);

       //这里要注意的是改变了四次状态,而只打印了三次,是因为Observable类中有是否变化这一判断

       watched.chageData("wo de lao po");

       watched.chageData("wo de lao po");

       watched.chageData("my wife");

       watched.chageData("我的老婆");

    }

}

 

你可能感兴趣的:(观察者模式)