您的位置:首页 > 编程语言 > Java开发

java 设计模式 观察者模式

2015-07-23 00:43 411 查看
观察者模式第一版:不断循环

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();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: