package com.bj.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) {
this.time = time;
this.loc = loc;
this.source = source;
}
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;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
class Child implements Runnable {
private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();
public void addWakenUpListener(WakenUpListener l) {
wakenUpListeners.add(l);
}
public void wakeUp() {
for (int i = 0; i < wakenUpListeners.size(); i++) {
WakenUpListener l = wakenUpListeners.get(i);
l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(),
"bed", this));//多态 父类WakenUpListener 指向子类对象 Dad GrandFather
}
}
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 wakenUpEvent) {
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();
//load到内存后 静态初始化只执行一遍
static {
try {
props.load(Test.class.getClassLoader().getResourceAsStream(
"observer.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
return props.getProperty(key);
}
class CryEvent {
}
abstract class Event {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
OOAD.rar_设计模式 例
共27个文件
class:20个
java:3个
properties:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 167 浏览量
2022-09-20
16:17:40
上传
评论
收藏 14KB RAR 举报
温馨提示
用observer设计模式实现一个小例子程序 模拟awt的事件监听机制
资源推荐
资源详情
资源评论
收起资源包目录
OOAD.rar (27个子文件)
OOAD
bin
observer.properties 77B
com
bj
observer
Test.class 1KB
WakenUpEvent.class 1KB
Dad.class 634B
Child.class 1KB
WakenUpListener.class 178B
awt
Test.class 720B
ActionListener.class 184B
TestFrame$1.class 729B
MyActionListener2.class 673B
Button.class 1KB
ActionEvent.class 608B
TestFrame$MyActionListener.class 1018B
TestFrame.class 1KB
MyActionListener.class 670B
TestFrame$MyActionListener2.class 1023B
Dog.class 637B
PropertyMgr$CryEvent.class 475B
PropertyMgr$Event.class 466B
PropertyMgr.class 1KB
GrandFather.class 649B
src
observer.properties 77B
com
bj
observer
awt
TestFrame.java 1KB
Test.java 1KB
Test.java 3KB
.project 380B
.classpath 232B
共 27 条
- 1
资源评论
JaniceLu
- 粉丝: 78
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功