我是靠谱客的博主 敏感航空,最近开发中收集的这篇文章主要介绍java设计模式事件处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package com.bjsxt.dp.observer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;




class WakenUpEvent {
private long time;
private String loc;
private Child source;

public WakenUpEvent(long time, String loc, Child source) {
super();
this.time = time;
this.loc = loc;
this.source = source;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Child getSource() {
return source;
}
public void setSource(Child source) {
this.source = source;
}
}


class Child implements Runnable {
private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();


public void addWakenUpListener(WakenUpListener l) {
wakenUpListeners.add(l);
}

void wakeUp() {
for(int i=0; i<wakenUpListeners.size(); i++) {
WakenUpListener l = wakenUpListeners.get(i);
l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
}
}


public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

this.wakeUp();
}

}


class Dad implements WakenUpListener {


public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
System.out.println("feed child");
}

}


class GrandFather  implements WakenUpListener {


public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
System.out.println("hug child");
}

}


class Dog implements WakenUpListener {


public void ActionToWakenUp(WakenUpEvent arg0) {
System.out.println("wang wang ...");
}

}


interface WakenUpListener {
public void ActionToWakenUp(WakenUpEvent wakenUpEvent);
}


public class Test {
public static void main(String[] args) {
Child c = new Child();


String[] observers = PropertyMgr.getProperty("observers").split(",");


for(String s : observers) {
try {
c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}


new Thread(c).start();
}
}


class PropertyMgr {
private static Properties props = new Properties();

static {
try {
props.load(Test.class.getClassLoader().getResourceAsStream("com/bjsxt/dp/observer/observer.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}

public static String getProperty(String key) {
return props.getProperty(key);
}
}


class CryEvent {
}


abstract class Event {


}

最后

以上就是敏感航空为你收集整理的java设计模式事件处理的全部内容,希望文章能够帮你解决java设计模式事件处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部