首页 > 其他 > 详细

观察者模式

时间:2015-03-28 10:02:37      阅读:197      评论:0      收藏:0      [点我收藏+]

观察者模式就是:

1、所有的观察者需要被通知,实现了一个接口,供主题调用

2、所有的主题都有一个观察者列表,主题通过调用观察者实现的接口通知观察者。

 

代码结构(inside代表使用java.util, self使用自定义的接口):

技术分享

interface DisplayElement:

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:28
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public interface DisplayElement {
    public void display();
}

interface subject:

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.self;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:25
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public interface Subject {
    public void registerObserver(Observer o) ;

    public void removeObserver(Observer o);

    public void notifyObserver();
}

interface Observer:

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.self;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:26
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public interface Observer {
    public void update(float temp, float humidity, float pressure);
}
WeatherData
/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.self.impl;

import com.misuosi.dp.observer.self.Observer;
import com.misuosi.dp.observer.self.Subject;

import java.util.ArrayList;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:30
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public class WeatherData implements Subject {

    private ArrayList observers;

    private float temperature;

    private float humidity;

    private float pressure;

    public WeatherData() {
        observers = new ArrayList();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

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

    @Override
    public void notifyObserver() {
        for (int i = 0; i< observers.size(); i++) {
            Observer observer = (Observer) observers.get(i);
            observer.update(temperature, humidity, pressure);
        }
    }

    public void measurementsChanged() {
        notifyObserver();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}

 CurrentConditionsDisplay

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.self.impl;

import com.misuosi.dp.observer.DisplayElement;
import com.misuosi.dp.observer.self.Observer;
import com.misuosi.dp.observer.self.Subject;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:41
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    private float temperature;

    private float humidity;

    private Subject weatherData;

    public CurrentConditionsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void update(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        display();
    }

    @Override
    public void display() {
        System.out.println("Current conditions:" + temperature
        + "F degrees and "+ humidity + "% humidity");
    }
}

  

使用jdk内置的观察者接口:

WeatherData

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.inside;

import java.util.Observable;

/**
 * Description		:
 * <br><br>Time		: 2015/3/28 8:03
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public class WeatherData extends Observable{

    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {}

    public void measurementsChanged() {
        setChanged();
        notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    public float getTemperature() {
        return temperature;
    }

    public float getHumidity() {
        return humidity;
    }

    public float getPressure() {
        return pressure;
    }
}

CurrentConditionsDisplay

/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer.inside;

import com.misuosi.dp.observer.DisplayElement;

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

/**
 * Description		:
 * <br><br>Time		: 2015/3/28 8:15
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    Observable observable;
    private float temperature;
    private float humidity;

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

    @Override
    public void update(Observable obs, Object arg) {
        if(obs instanceof WeatherData){
            WeatherData weatherData = (WeatherData) obs;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }

    @Override
    public void display() {
        System.out.println("Current conditions:" + temperature
        + "F degrees and " + humidity + "% humidity");
    }
}

 

测试类:

WeatherStationSelf
/*
 * Copyright (c) 2014 
 * 广州米所思信息科技有限公司(Guangzhou Mythos Information technology co., LTD) 
 * All rights reserved.
 */
package com.misuosi.dp.observer;

import com.misuosi.dp.observer.self.impl.CurrentConditionsDisplay;
import com.misuosi.dp.observer.self.impl.WeatherData;
import org.junit.Test;

/**
 * Description		:
 * <br><br>Time		: 2015/3/27 23:45
 *
 * @author ICE
 * @version 1.0
 * @since 1.0
 */
public class WeatherStationSelf {

    @Test
    public void test() {
        WeatherData weatherData = new WeatherData();

        CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(82, 70, 29.2f);
        weatherData.setMeasurements(78, 90, 29.2f);
    }
}

  

 

观察者模式

原文:http://www.cnblogs.com/yingbing/p/4373577.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!