观察者模式

来源:互联网 发布:mac优酷缓存视频转换 编辑:程序博客网 时间:2024/04/20 17:24
  1. /*
  2.  * 观察者模式
  3.  * 适用于一vs多的状况,当一个对象改变时,需要通知其他多个对象,而另外的多个对象各自有各自的独特行为!
  4.  * 观察者模式从uml中很容易看出特点!有点类似于网络中的组播方式:多个设备加入组播组(多个对象成为观察者),一个设备发
  5.  * 消息时,组播组中所有设备都将接收到消息(一个对象发通知,其他收到通知),设备收到消息后,怎么显示是自己的事情(如msn,qq等各种显示方式一样)
  6.  * (其他对象收到通知时,也各自有自己的处理方式,扔掉or显示or...之类处理)
  7.  * 
  8.  * Observer定义成一个接口,里面有两个方法(update,exitsubject退出订阅),应该还需要增加一个加入订阅,当时忘记了(只在具体实现接口
  9.  * 初始化时注册加入订阅)
  10.  * 
  11.  * weatherDisplay1 与 weatherDisplay2是两个具体实现接口Observer的类
  12.  * 初始化时先注册加入到subject中,然后实现update和exitsubject功能
  13.  * 
  14.  * 而subject是一个接口,用于被Observer注册订阅(相当于1VS多中的“1”),其中方法有加入,通知,退出等
  15.  * 而weatherData是实现subject的一个类,具体实现上面的方法。
  16.  * 
  17.  * 测试程序中可以看到观察者模式是怎样工作的,先加入订阅组,在更新时更新所有组内成员信息,然后类成员自己实现自己处理方式(例子中只是简单显示)
  18.  * 另外,observer可以退出订阅,这时发送的通知就不会通知到该observer
  19.  * 
  20.  */
  21. package observer_p;
  22. public class Test_main {
  23.     /**
  24.      * @param args
  25.      */
  26.     public static void main(String[] args) {
  27.         // TODO 自动生成方法存根
  28.         
  29.         weatherData main_s = new weatherData();
  30.         Observer o1 = new weatherDisplay1(main_s);
  31.         Observer o2 = new weatherDisplay2(main_s);
  32.         
  33.         
  34.         main_s.measurementChange(12,13);
  35.         System.out.println("----------------------------------");
  36.         main_s.measurementChange(15,16);
  37.         System.out.println("----------------------------------");
  38.         
  39.         o1.exitSubject(main_s);
  40.         main_s.measurementChange(18,19);
  41.         
  42.     }
  43. }
  44. //Observer .java
  45. /**
  46.  * 
  47.  */
  48. package observer_p;
  49. /**
  50.  * @author Administrator
  51.  *
  52.  */
  53. public interface Observer {
  54.     
  55.     void update(float t,float h);
  56.     void exitSubject(Subject s);
  57. }
  58. //weatherDisplay1 .java
  59. package observer_p;
  60. public class weatherDisplay1 implements Observer{
  61.     
  62.     
  63.     private float temperature;
  64.     private float humidity;
  65.     private Subject main_s;
  66.     
  67.     public weatherDisplay1(Subject s){
  68.             
  69.         s.registerObserver(this);
  70.         this.main_s = s;
  71.         
  72.     }
  73.     
  74.     public void update(float t,float h){
  75.         
  76.         temperature = t;
  77.         humidity = h;
  78.         display1();
  79.     }
  80.     
  81.     public void exitSubject(Subject s){
  82.         s.removeObserver(this);
  83.     }
  84.     
  85.     public void display1(){
  86.         
  87.         System.out.println("I am Display1 temperature :" + temperature +
  88.                             " humidity :" + humidity);
  89.         
  90.         
  91.     }
  92.     
  93. }
  94. //weatherDisplay2 .java
  95. package observer_p;
  96. public class weatherDisplay2 implements Observer{
  97.     
  98.     private float temperature;
  99.     private float humidity;
  100.     private Subject main_s;
  101.     
  102.     public weatherDisplay2(Subject s){
  103.             
  104.         s.registerObserver(this);
  105.         this.main_s = s;
  106.         
  107.     }
  108.     
  109.     public void update(float t,float h){
  110.         
  111.         temperature = t;
  112.         humidity = h;
  113.         display2();
  114.     }
  115.     
  116.     public void exitSubject(Subject s){
  117.         s.removeObserver(this);
  118.     }
  119.     
  120.     public void display2(){
  121.         
  122.         System.out.println("I am Display2 temperature :" + temperature +
  123.                             " humidity :" + humidity);
  124.         
  125.         
  126.     }
  127. }
  128. //Subject .java
  129. /**
  130.  * 
  131.  */
  132. package observer_p;
  133. /**
  134.  * @author Administrator
  135.  *
  136.  */
  137. public interface Subject {
  138.     
  139.     void registerObserver(Observer o);
  140.     void removeObserver(Observer o);
  141.     void notifyObserver();
  142.     
  143. }
  144. //weatherData .java
  145. package observer_p;
  146. import java.util.*;
  147. public class weatherData implements Subject{
  148.     
  149.     private ArrayList observer_list;
  150.     private float temperature;
  151.     private float humidity;
  152.     public float getTemperature(){
  153.         return temperature;
  154.     }
  155.     
  156.     public float getHumidity(){
  157.         return humidity;
  158.     }
  159.     
  160.     public weatherData(){
  161.         observer_list = new ArrayList();            //注意初始化
  162.     }
  163.     
  164.     public void registerObserver(Observer o){
  165.         
  166.         observer_list.add(o);
  167.         
  168.     }
  169.     
  170.     public void removeObserver(Observer o){
  171.         
  172.         for(int i = 0;i<observer_list.size();i++)
  173.         {
  174.             if(observer_list.get(i) == o)
  175.             {
  176.                 observer_list.remove(i);
  177.             }
  178.         }
  179.     }
  180.     
  181.     public void notifyObserver(){
  182.         
  183.         for(int i = 0;i<observer_list.size();i++)
  184.         {
  185.             Observer o_tmp = (Observer)observer_list.get(i);
  186.             o_tmp.update(temperature,humidity);
  187.         }
  188.     }
  189.     
  190.     public void measurementChange(float t,float h){
  191.         
  192.         temperature = t;
  193.         humidity = h;
  194.         notifyObserver();
  195.     }
  196.     
  197. }
原创粉丝点击