java 设计模式 观察者模式

观察者模式第一版:不断循环

package luis;


class Child implements Runnable{
private boolean wakenUp=false;
public boolean isWakenUp() {
return wakenUp;
}
public void setWakenUp(boolean wakenUp) {
this.wakenUp = wakenUp;
}
void wakeUp(){
wakenUp=true;
}
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.wakeUp();
}
}


class Dad implements Runnable{
Child c;
public Dad(Child c){
this.c=c;
}
@Override
public void run() {
while(!c.isWakenUp()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
feed(c);
}
private void feed(Child c2) {
System.out.println("feed child");
}
}


public class Test {
public static void main(String[] args) {
Child d=new Child();
new Thread(d).start();
new Thread(new Dad(d)).start();
}

}


补充,线程wait 调用只能锁定一个对象再在那个对象上调用(wait 虽然不耗资源)

nodify 才能叫醒wait


第二版本,主动

package luis;


class Child implements Runnable{
private Dad d;

public Child(Dad d) {
this.d = d;
}
private boolean wakenUp=false;
public boolean isWakenUp() {
return wakenUp;
}
public void setWakenUp(boolean wakenUp) {
this.wakenUp = wakenUp;
}
void wakeUp(){
wakenUp=true;
d.feed(this);
}
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.wakeUp();
}
}


class Dad {
public void feed(Child c2) {
System.out.println("feed child");
}
}


public class Test {
public static void main(String[] args) {
Child c=new Child(new Dad());
new Thread(c).start();
}

}


第三版:事件驱动

package luis;


import java.util.ArrayList;
import java.util.List;


class WakenUpEvent {
public WakenUpEvent(long time, String loc, Child source) {
super();
this.time = time;
this.loc = loc;
this.source = source;
}


private long time;


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;
}


private String loc;
private Child source;
}


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


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


void wakeUp() {
for (WakenUpListener l : wakenUpListeners) {
l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
}
}


@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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");
}
}


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


public class Test {
public static void main(String[] args) {
Dad d=new Dad();
GrandFather gf=new GrandFather();
Child c = new Child();
c.addWakenUpListener(d);
c.addWakenUpListener(gf);
new Thread(c).start();
}


}


补充:从配置文件读取的时候路径上一定要加上包名。

方法四:配置文件

package luis;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


class WakenUpEvent {
public WakenUpEvent(long time, String loc, Child source) {
super();
this.time = time;
this.loc = loc;
this.source = source;
}


private long time;


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;
}


private String loc;
private Child source;
}


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


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


void wakeUp() {
for (WakenUpListener l : wakenUpListeners) {
l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
}
}


@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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");
}
}


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


public class Test {
public static void main(String[] args) {

Properties props=new Properties();
try {
props.load(Test.class.getClassLoader()
.getResourceAsStream("luis/observer.properties"));
//补充:从配置文件读取的时候路径上一定要加上包名。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//source 目录编译好的东西会放到bin 目录下,bin 就是项目的classpath;
String ss=props.getProperty("observers");
String[] a =ss.split(",");
Child c = new Child();
new Thread(c).start();
for (String s:a){
try {
c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}



单例缓存的写法:

class PropertyMgr{
//单例
private static Properties props=new Properties();
static {//靜態初始化代碼,靜態初始化代碼只執行一遍。在内存里就这么一个对象,再次读取直接读内存不读硬盘。
try {
props.load(Test.class.getClassLoader()
.getResourceAsStream("luis/observer.properties"));
//补充:从配置文件读取的时候路径上一定要加上包名。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getPropty(String key){
return props.getProperty(key);
}
}



public class Test {
public static void main(String[] args) {

Properties props=new Properties();
try {
props.load(Test.class.getClassLoader()
.getResourceAsStream("luis/observer.properties"));
//补充:从配置文件读取的时候路径上一定要加上包名。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//source 目录编译好的东西会放到bin 目录下,bin 就是项目的classpath;
String ss=PropertyMgr.getPropty("observers");//静态方法通过类名调用,节约资源不用new对象。就在内存里的区域
String[] a =ss.split(",");
Child c = new Child();
new Thread(c).start();
for (String s:a){
try {
c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值