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();
}
}
}
}
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();
}
}
}
}
相关文章推荐
- java 7 新特性/switch/枚举类型/二进制/字面量
- Inside the Java Virtual Machine 读书笔记一
- eclipse打不开闪退解决方式
- Ant编译java项目时无法导入rt.jar中的类
- 对Java中线程的深入探讨
- 在eclipse中调试运行springside4 showcase
- 100个高质量Java开发者博客
- 表达树构造(java)
- java多线程编程(一)
- MyEclipse10.7使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- 7月22日本人正在学习Java每天所学晚上都会在博客更新,如果错了可以在评论指出。
- Java并发编程:volatile关键字解析
- spring(12) Spring的通过工厂方法配置Bean
- java常用类源代码探究(二):装箱类
- Java学习01
- java学习笔记(三) FileStream
- 用java实现 小孩围圈数三退一游戏
- java:ArrayList集合与HashSet集合元素重复性探讨
- java中时分秒的取得与转换
- java-多线程安全问题