henu_zhangyang 阅读(122) 评论(0)

观察者模式:定义了对象之间的一对多依赖,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

设计原则:

1.为交互对象之间的松耦合设计

 

注意:java本身自带了这种模式的接口。

 

 

具体实现如下:

 

 

/**
 *主题接口
 */
public interface Subject {
    void registerObserver(Observer observer);
    void removeObserver(Observer observer);
    void notifyObservers();
}

 

 

/**
 * 观察者接口
 */
public interface Observer {
    void update(String params);
}
/**
 * 具体主题的实现
 */
public class WeatherData implements Subject {
    private List<Observer> observers;
    private String params; //需要发布的数据

    public WeatherData() {
        this.observers = new ArrayList<Observer>();
    }

    @Override
    public void registerObserver(Observer observer) {
        if (observer != null) {
            this.observers.add(observer);
        }
    }

    @Override
    public void removeObserver(Observer observer) {
        int index = observers.indexOf(observer);
        if (index > 0) {
            observers.remove(index);
        }
    }

    /**
     * 通过此方法将数据发布给各订阅者
     * */
    @Override
    public void notifyObservers() {
        for(Observer observer: observers){
            observer.update(params);
        }
    }

    /**
     * 需要发布的数据状态有改变
     * */
    public void setChange(){
        notifyObservers();
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
        setChange();
    }
}

  

/**
 * 观察者实现
 */
public class CustomerObserver implements Observer {

    private String params;  //订阅的数据
    private Subject subject;

    public CustomerObserver(Subject subject) {
        this.subject = subject;
        this.subject.registerObserver(this); //此处维护订阅的关系
    }

    @Override
    public void update(String params) {
        this.params = params;
        System.out.println("display params:" + params);
    }
}

 

/**
 * 测试类
 */
public class TestObserver {
    public static void main(String[] args) {
        WeatherData subject = new WeatherData();
        Observer observer1 = new CustomerObserver(subject);
        Observer observer2 = new CustomerObserver(subject);
        subject.setParams("data1");
        subject.setParams("data2");
    }

}

 

 

 

通过java 自身的接口实现观察者模式

 

import java.util.Observable;

/**
 *继承java的类,不需要实现注册,和取消注册等方法,已经实现
 */
public class WeatherData extends Observable {

    private String params;     //需要发布的数据

    public void statuChange(){
        setChanged();   //注意此方法一定要调用,改变发布数据的状态
//        notifyObservers();  //java自身实现
        notifyObservers(new String("这个方法,将此对象发布到观察者对象中"));
    }


    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
        statuChange();      //状态改变
    }

    @Override
    public String toString() {
        return "WeatherData{" +
                "params='" + params + '\'' +
                '}';
    }
}

 

 

import java.util.Observable;
import java.util.Observer;

/**
 * 观察者
 */
public class CustomerObserver   implements Observer {

    private Observable observable;

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

    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof WeatherData){
            //此处获取发布的数据
            System.out.println(o);
        }
        if(arg!=null){  //这个对象是发布方法传过来的
            System.out.println("--------------" +arg);
        }
    }
}

 

 

/**
 * Created by Administrator on 2017/11/18.
 */
public class TestJavaObserver {
    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        CustomerObserver customerObserver = new CustomerObserver(weatherData);
        weatherData.setParams("参数");
    }
}