设计模式——观察者模式

1.什么是观察者模式

观察者模式,简单理解是目标对象和观察者之间的联动模式,当目标对象发生表示时,会通知到观察者(一个或者多个)。

 

2.观察者模式的应用场景

2.1 当一个对象改变时,同时需要改变其他对象,但不清楚有多少个对象待改变时。

2.2   当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。

 

3.java jdk中提供的Observable类和Observer接口

WeatherConet.java

package com.asiainfo.test.observerjdk;

/**
 * 天气状态
 * @author hasee
 *
 */
public class WeatherConet {
    static String SUNNY = "阳光灿烂";
    static String RAINY = "下雨了";
    static String SONW = "下雪了";
}

ConcreteWeatherSubject.java,继承jdk提供的Observable 类

package com.asiainfo.test.observerjdk;

import java.util.Observable;

/**
 * 目标对象,保存了所有的注册的观察者对象,并且提供观察者对象的注册和删除的方法
 * @author luke
 *
 */
public class ConcreteWeatherSubject extends Observable {
    private String weatherContent;
    
    public String getWeatherContent() {
        return weatherContent;
    }
    
    
    //当目标对象状态发生变化时,通知对应的对象
    public void setWeatherContent(String weatherContent) {
        this.weatherContent = weatherContent;
        this.setChanged();
        this.notifyObservers(weatherContent);
    }
    
     
}

 

ConcreteWeatherObserver.java,实现jdk提供的Observer 接口

package com.asiainfo.test.observerjdk;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

/**
 * 观察者实现类,提供update方法,供目标对象发生变化时调用,通知观察者
 * @author hasee
 *
 */
public class ConcreteWeatherObserver implements Observer {
    static Map<String,String> observerMap;
    static {
        observerMap = new LinkedHashMap<String,String>();
        observerMap.put("Mum", "妈妈");
        observerMap.put("GrilFriend", "女朋友");
    }
    //观察者的名称
    private String observerName;
    //观察者获取的天气内容
    private String weatherContent;
    //提醒内容
    private String remindThing;
    
    
    @Override
    public void update(Observable o, Object arg) {
        ConcreteWeatherSubject subject = (ConcreteWeatherSubject)o;
        String weatherContent = subject.getWeatherContent();
        System.out.println(observerName + "收到了" + weatherContent + "," + remindThing);
    }


    public String getObserverName() {
        return observerName;
    }


    public void setObserverName(String observerName) {
        this.observerName = observerName;
    }


    public String getWeatherContent() {
        return weatherContent;
    }


    public void setWeatherContent(String weatherContent) {
        this.weatherContent = weatherContent;
    }


    public String getRemindThing() {
        return remindThing;
    }


    public void setRemindThing(String remindThing) {
        this.remindThing = remindThing;
    }
    
}

Client.java 测试类

package com.asiainfo.test.observerjdk;
//测试类
public class Client {
    public  static void main(String[] args) {
        //创建目标对象
        ConcreteWeatherSubject subject = new ConcreteWeatherSubject();
        
        //创建观察者对象
        ConcreteWeatherObserver observerMum = new ConcreteWeatherObserver();
        observerMum.setObserverName(observerMum.observerMap.get("Mum"));
        observerMum.setRemindThing("去逛街");
        
        ConcreteWeatherObserver observerGril = new ConcreteWeatherObserver();
        observerGril.setObserverName(observerMum.observerMap.get("GrilFriend"));
        observerGril.setRemindThing("去约会");
        //注册观察者
        subject.addObserver(observerMum);
        subject.addObserver(observerGril);
        
        //目标对象发布状态
        subject.setWeatherContent("下雨了");
        
    }
}

 

4.可以自己定义观察者的接口和目标对象类

4.1  WeatherSubject.java

 1 package com.asiainfo.test.observer;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  * 天气目标对象,知道观察自己的观察者,并且提供对观察者添加和删除的方法
 8  * @author luke
 9  *
10  */
11 public class WeatherSubject {
12     //用于保存所以的观察者
13     private List<WeatherObserver> observers = new ArrayList();
14     
15     //提供对观察者进行添加或者删除的方法
16     public void attach (WeatherObserver observer) {
17         observers.add(observer);
18     }
19     
20     public void delete(WeatherObserver observer) {
21         observers.remove(observer);
22     }
23     
24     //如果目标对象发生改变时,通知所有注册的观察者对象
25     protected void notifyObservers() {
26         for (int i = 0; i < observers.size(); i++) {
27             WeatherObserver observer = observers.get(i);
28             observer.update(this);
29         }
30     }
31 }
View Code

4.2  ConcreteWeatherSubject.java

 1 package com.asiainfo.test.observer;
 2 
 3 /**
 4  * 
 5  * @author luke
 6  *
 7  */
 8 public class ConcreteWeatherSubject extends WeatherSubject {
 9     //目标对象的状态
10     private String weatherContent;
11     
12     public String getWeatherContent() {
13         return weatherContent;
14     }
15 
16     public void setWeatherContent(String weatherContent) {
17         this.weatherContent = weatherContent;
18         //通知所有的观察者
19         this.notifyObservers();
20     }
21     
22     
23     
24 }
View Code

4.3  WeatherObserver.java

1 package com.asiainfo.test.observer;
2 
3 public interface WeatherObserver {
4     //更新观察者的状态,传入目标对象方便获取对应的观察者的引用
5     public void update(WeatherSubject subject);
6 }
View Code

4.4  ConcreteWeatherObserver.java

 1 package com.asiainfo.test.observer;
 2 
 3 public class ConcreteWeatherObserver implements WeatherObserver {
 4     //观察者的名称
 5     private String observerName;
 6     //观察者的内容,从目标处获取
 7     private String subContent;
 8     //提醒的内容
 9     private String remindThings;
10     
11     /**
12      * 获取目标类的状态并同步到观察者的状态中
13      */
14     @Override
15     public void update(WeatherSubject subject) {
16         ConcreteWeatherSubject concreteSub = (ConcreteWeatherSubject)subject;
17         subContent = concreteSub.getWeatherContent();
18         System.out.println(observerName + "收到了信息:" + subContent + "," + remindThings);
19     }
20 
21     public String getObserverName() {
22         return observerName;
23     }
24 
25     public void setObserverName(String observerName) {
26         this.observerName = observerName;
27     }
28 
29     public String getSubContent() {
30         return subContent;
31     }
32 
33     public void setSubContent(String subContent) {
34         this.subContent = subContent;
35     }
36 
37     public String getRemindThings() {
38         return remindThings;
39     }
40 
41     public void setRemindThings(String remindThings) {
42         this.remindThings = remindThings;
43     }
44 
45     
46 }
View Code

4.4  Client.java测试类

 1 package com.asiainfo.test.observer;
 2 
 3 //测试类
 4 public class Client {
 5     public static void main(String[] args) {
 6         //1.创建目标对象
 7         ConcreteWeatherSubject subject = new ConcreteWeatherSubject();
 8         
 9         //2.创建观察者对象
10         ConcreteWeatherObserver mum = new ConcreteWeatherObserver();
11         mum.setObserverName("黄明的妈妈");
12         mum.setRemindThings("适合去超市购物");
13         
14         ConcreteWeatherObserver gf = new ConcreteWeatherObserver();
15         gf.setObserverName("黄明的女朋友");
16         gf.setRemindThings("适合去谈恋爱");
17         
18         //3.注册观察者1
19         subject.attach(mum);
20         subject.attach(gf);
21         subject.setWeatherContent("天气晴朗,气温28°");
22         
23     }
24 }
View Code

 

Client.java

 

posted on 2017-02-04 12:56  lukelin1989  阅读(149)  评论(0编辑  收藏  举报

导航