首页 技术 正文
技术 2022年11月15日
0 收藏 524 点赞 3,647 浏览 1884 个字

一、概述

1.目标:读取properties文件改成单例模式

二、代码

1.Test.java

 class WakenUpEvent{     private long time;
private String location;
private Child source; public WakenUpEvent(long time, String location, Child source) {
super();
this.time = time;
this.location = location;
this.source = source;
} public long getTime() {
return time;
} public void setTime(long time) {
this.time = time;
} public String getLocation() {
return location;
} public void setLocation(String location) {
this.location = location;
} 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 wul){
wakenUpListeners.add(wul);
}
public void wakeUp(){
for(int i = 0; i < wakenUpListeners.size(); i++){
WakenUpListener l = wakenUpListeners.get(i);
l.actionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
}
} @Override
public void run() {
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
wakeUp();
}
} interface WakenUpListener {
public void actionToWakenUp(WakenUpEvent e);
} class Dad implements WakenUpListener { public void actionToWakenUp(WakenUpEvent e) {
System.out.println("Fedd the child");
} } class GrandFather implements WakenUpListener { public void actionToWakenUp(WakenUpEvent e) {
System.out.println("抱孩子");
} } class PropertiesMgr{ private static Properties props = new Properties(); //只加载一次,但如果properties有更新,它不会反馈
static {
try {
props.load(Test.class.getClassLoader().getResourceAsStream("com/tong/observer/observer.properties"));
} catch (Exception e) {
e.printStackTrace();
}
} public static String getProperty(String key){
return props.getProperty(key);
}
}
public class Test { public static void main(String[] args) { Child c = new Child(); String [] observers = PropertiesMgr.getProperty("observers").split(","); for(String s : observers){
try {
c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
new Thread(c).start();
}
}

三、运行结果

Java-马士兵设计模式学习笔记-观察者模式-读取properties文件改成单例模式

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898