Observer 观察者模式

来源:互联网 发布:yum pip3 编辑:程序博客网 时间:2024/06/07 12:24

源代码

 

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()+"洗澡");elseSystem.out.println("爸爸没事干");}}class mother implements WakeUpEventListener{public void actionPerfrom(WakeUpEvent e) {if(e.getDoWhat().equals(DoWhatWhenWakeUp.Suck))System.out.println("妈妈给"+((Child)e.getActionSource()).getName()+"喂奶");elseSystem.out.println("妈妈没事干");}}class brother implements WakeUpEventListener{public void actionPerfrom(WakeUpEvent e) {if(e.getDoWhat().equals(DoWhatWhenWakeUp.Play))System.out.println("哥哥跟"+((Child)e.getActionSource()).getName()+"一起玩");elseSystem.out.println("哥哥没事干");}}public class Observer {/** * @描述:事件枚举类 */public enum DoWhatWhenWakeUp{Suck,Wash,Play}public static void main(String[] args) {//小孩叫rain,醒了后要DoWhatWhenWakeUp.playChild 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"));}}


 

原创粉丝点击