观察者模式

1.什么是观察者模式

   观察者模式定义了对象之间的一对多依赖,即一个对象的状态发生变化时,它的所以依赖者都会收到通知并且自动更新。


2.观察者的要点

  • 主题对象
              主题对象是真正拥有数据的对象。
  • 观察者
             依赖主题对象,在主题数据发生变化时更新。
  •  主题和观察者之间的关系
            观察者模式中有一个对象对应有多个观察者,并且多个观察者都依赖主题告诉他们状态或数据何时发生了改变。
            主题对象和观察者之间用接口进行交互,让主题对象和观察者之间实现松耦合。

3.具体示例

        主题接口
package com.mingyue.weather.intf;

public interface Subject
{
	/**
	 * 注册一个观察者
	 * @param o 观察者
	 */
	public void registerObserver(Observer o);
	
	/**
	 * 注销一个观察者
	 * @param o 观察者
	 */
	public void removeObserver(Observer o);
	
	/**
	 * 通知观察者对象状态变更
	 */
	public void notifyObservers();
}


        观察者接口

package com.mingyue.weather.intf;

public interface Observer
{
	/**
	 * 根据对象状态发生变化,更新自己
	 * @param arg1 所观察对象的状态1
	 * @param arg2 所观察对象的状态2
	 * @param arg3 所观察对象的状态3
	 */
	public void update(float arg1, float arg2, float arg3);
}


package com.mingyue.weather.intf;

public interface Displayment
{
	/**
	 * 展示对象状态
	 */
	public void display();
}


        主题对象
package com.mingyue.weather.bean;

import java.util.ArrayList;
import java.util.List;

import com.mingyue.weather.intf.Observer;
import com.mingyue.weather.intf.Subject;

/**
 * @author dell
 *
 */
public class WeatherData implements Subject
{
	/**
	 * 温度状态 
	 */
	private float temp = Float.MIN_VALUE; 
	
	/**
	 * 湿度状态
	 */
	private float humdity = Float.MIN_VALUE; 
	
	/**
	 * 气压状态
	 */
	private float presure = Float.MIN_VALUE; 
	
	/**
	 * 该对象的观察者列表
	 */
	List<Observer> observers;
	
	public WeatherData()
	{
		observers = new ArrayList<Observer>(1);
	}

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

	@Override
    public void removeObserver(Observer o)
    {
		int index = observers.indexOf(o);
		observers.remove(index);
    }

	@Override
    public void notifyObservers()
    {
		for (Observer o : observers)
		{
			o.update(this.temp, this.humdity, this.presure);
		}
    }
	
	/**
	 * 对外接口
	 */
	public void valuesChanged()
	{
		notifyObservers();
	}
	
	
	/**
	 * 对外接口
	 * @param temp 属性1
	 * @param humdity 属性2
	 * @param presure 属性3
	 */
	public void setValues(float temp, float humdity, float presure)
	{
		this.temp = temp;
		this.humdity = humdity;
		this.presure = presure;
		valuesChanged();
	}
}


        观察者对象
package com.mingyue.weather.bean;

import com.mingyue.weather.intf.Displayment;
import com.mingyue.weather.intf.Observer;
import com.mingyue.weather.intf.Subject;

public class CurrentConditionDisplay implements Observer, Displayment
{
	/**
	 * 该观察者观察的对象
	 */
	Subject sb;
	
	/**
	 * 自己的属性1
	 */
	private float temp = Float.MIN_VALUE;
	
	/**
	 * 自己的属性2
	 */
	private float humdity = Float.MIN_VALUE;
	
	/**
	 * 自己的属性3
	 */
	private float presure = Float.MIN_VALUE;
	
	public CurrentConditionDisplay(Subject sb)
	{
		this.sb = sb;
		sb.registerObserver(this);
	}
	
	@Override
    public void display()
    {
		StringBuilder sb = new StringBuilder().append("The temp is ")
				.append(this.temp)
				.append(", The humdity is ")
				.append(this.humdity)
				.append(", The presure is ")
				.append(this.presure);
		
		System.out.println(sb.toString());
    }

	@Override
    public void update(float arg1, float arg2, float arg3)
    {
		this.temp = arg1;
		this.humdity = arg2;
		this.presure = arg3;
		
		display();
    }
	
}


        具体的客户端

package com.mingyue.weather;

import com.mingyue.weather.bean.CurrentConditionDisplay;
import com.mingyue.weather.bean.WeatherData;

public class WeatherObserverStation
{
	
	public static void main(String[] args)
	{
		WeatherData data = new WeatherData();
		
		CurrentConditionDisplay displayer = new CurrentConditionDisplay(data);
		
		data.setValues(1.0F, 2.3F, 3.33F);
		data.setValues(2.0F, 22.3F, 32.33F);
		data.setValues(3.0F, 23.3F, 33.33F);
	}
	
}

客户端输出:

The temp is 1.0, The humdity is 2.3, The presure is 3.33
The temp is 2.0, The humdity is 22.3, The presure is 32.33
The temp is 3.0, The humdity is 23.3, The presure is 33.33


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值