Observer 观察者模式

源代码

 

package com.rain.Observer;
import java.util.ArrayList;
import com.rain.Observer.Observer.DoWhatWhenWakeUp;
import com.rain.Utils.ProperUtils;

/**
 * 
 *	@描述:观察者模式:事件,事件源,监听者,响应监听(事件和响应都是课扩展的)
 *	
 */
/**
 * 
 * @描述:小孩睡觉(醒了派发事件,由监听者接受事件 做出响应)
 *
 */
class Child implements Runnable{
	private String name;
	private ArrayList<WakeUpEventListener> listeners=new ArrayList<WakeUpEventListener>();
	private DoWhatWhenWakeUp doWhatWhenWakeUp;
	
//构造函数(起名字,规定醒来要干嘛)
	public Child(String name, DoWhatWhenWakeUp dowhat) {
		this.name = name;
		this.doWhatWhenWakeUp = dowhat;
		}
//获取小孩的名字		
	public String getName() {
		return name;
	}
//为小孩添加事件监听者
	public void addWakeUpListener(WakeUpEventListener l){
		this.listeners.add(l);
	}
//醒来后通知所有监听者,派发事件		
	public void wakeUp(){
		for(WakeUpEventListener l:listeners){
			WakeUpEvent e=new WakeUpEvent(System.currentTimeMillis(), this, this.doWhatWhenWakeUp);
			l.actionPerfrom(e);
		}
	}
//模拟睡觉
	public void run() {
		for(int i=0;i<4;i++){
			System.out.println(this.getName()+"在睡觉");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(this.getName()+"醒了");
		System.out.println(this.getName()+"有"+listeners.size()+"监听者");
		this.wakeUp();
	}

}

/**
 * @描述:事件类(小孩醒了要派发一个事件)
 * @属性:时间,谁,要干什么等信息
 */
class WakeUpEvent{
	
	private long when;
	private Object actionSource;
	private DoWhatWhenWakeUp doWhat;
	
	
	public WakeUpEvent(long when, Object actionSource,DoWhatWhenWakeUp doWhat) {
		this.when = when;
		this.actionSource = actionSource;
		this.doWhat = doWhat;
	}
	
	public long getWhen() {
		return when;
	}
	public Object getActionSource() {
		return actionSource;
	}
	public DoWhatWhenWakeUp getDoWhat() {
		return doWhat;
	}
	
}

/**
 * @描述:监听者接口(这样凡是实现了这个接口的类都能对小孩进行监听,有扩展性)
 */
interface WakeUpEventListener{
	public void actionPerfrom(WakeUpEvent e);
}
class father implements WakeUpEventListener{

	public void actionPerfrom(WakeUpEvent e) {
		if(e.getDoWhat().equals(DoWhatWhenWakeUp.Wash))
			System.out.println("爸爸给"+((Child)e.getActionSource()).getName()+"洗澡");
		else
			System.out.println("爸爸没事干");
	}

}
class mother implements WakeUpEventListener{

	public void actionPerfrom(WakeUpEvent e) {
		if(e.getDoWhat().equals(DoWhatWhenWakeUp.Suck))
			System.out.println("妈妈给"+((Child)e.getActionSource()).getName()+"喂奶");
		else
			System.out.println("妈妈没事干");
	}
}
class brother implements WakeUpEventListener{

	public void actionPerfrom(WakeUpEvent e) {
		if(e.getDoWhat().equals(DoWhatWhenWakeUp.Play))
			System.out.println("哥哥跟"+((Child)e.getActionSource()).getName()+"一起玩");
		else
			System.out.println("哥哥没事干");
	}
}


public class Observer {

/**
 * @描述:事件枚举类
 */
public enum DoWhatWhenWakeUp{
	Suck,
	Wash,
	Play
}
	

public static void main(String[] args) {
	//小孩叫rain,醒了后要DoWhatWhenWakeUp.play
	Child c=new Child("rain", DoWhatWhenWakeUp.Play);
	
	//根据键值对获取Properties数据
	String[] observers= ProperUtils.getProperValues("com/rain/Observer/observer.properties","observer").split(",");
	
	//运用反射机制构造出监听器类
	for(String className:observers){
		System.out.println(className);
		try {
	//为小孩添加监听者
			c.addWakeUpListener((WakeUpEventListener) Class.forName(className).newInstance());
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
//启动线程
	new Thread(c).start();
		
	}

}


 

 

配置文件代码

observer=com.rain.Observer.father,com.rain.Observer.mother,com.rain.Observer.brother

 

读取配置文件的工具类代码:

package com.rain.Utils;
import java.io.IOException;
import java.util.Properties;
import com.rain.Observer.Observer;


public final class ProperUtils {

	public static Properties prop=new Properties();
//单例缓存	
//	static{
//		try {
//			prop.load(Observer.class.getClassLoader().getResourceAsStream("com/rain/Observer/observer.properties"));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	
	public static String getProperValues(String path,String key){
		try {
			prop.load(Observer.class.getClassLoader().getResourceAsStream(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return prop.getProperty(key);
	}
	
	public static void main(String[] args) {
		System.out.println(ProperUtils.getProperValues("com/rain/Observer/observer.properties","observer"));
	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值