浏览 3378 次
锁定老帖子 主题:事件驱动的比较
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-19
最后修改:2009-01-04
在java里面事件驱动是一种比较常见的设计模式,主要以awt控件为主!对于事件驱动我了解比较深刻的时候应该就是现在了,昨天刚刚听了一个高手将事件驱动,明白了一些道理,高手举的例子是一个小孩在睡觉,醒了之后,监护人去照顾他的场景模拟!
事件驱动标准的代码 package event.cn; import java.util.ArrayList; import java.util.List; /* 看护人 */ interface Keeper { public abstract void action(EventSource es); } class Father implements Keeper { @Override public void action(EventSource es) { System.out.println("Father do........"); } } class Mother implements Keeper { @Override public void action(EventSource es) { System.out.println("Monther do........"); } } class GrandFather implements Keeper { @Override public void action(EventSource es) { System.out.println("GrandFather do........"); } } class GrandMother implements Keeper { @Override public void action(EventSource es) { System.out.println("GrandMonther do........"); } } /* 被看护人 */ class Child implements Runnable { List<Keeper> keepers = new ArrayList<Keeper>(); EventSource es; public Child() { } public void addKeeper(Keeper k){ this.keepers.add(k); } public void wakeup() { for(Keeper k : this.keepers){ k.action(es); } } @Override public void run() { System.out.println("sleep ......"); try { new Thread().sleep(5000);// 5秒后醒来 this.wakeup(); } catch (InterruptedException e) { e.printStackTrace(); } } } /* 传递信息 */ class EventSource { private String something_msg; private Child child; public EventSource(String something_msg, Child child) { super(); this.something_msg = something_msg; this.child = child; } public String getSomething_msg() { return something_msg; } public void setSomething_msg(String something_msg) { this.something_msg = something_msg; } public Child getChild() { return child; } public void setChild(Child child) { this.child = child; } } public class EventTest { public static void main(String[] args) { Child child = new Child(); child.addKeeper(new Father()); child.addKeeper(new GrandFather()); new Thread(child).start(); } }
在听高手讲解之前,我只能写出这样的代码 package event.com; import java.util.ArrayList; import java.util.List; /* 看护人 */ abstract class Keeper{ public abstract void action(EventSource es); public void receive(EventSource es ){ List<Keeper> ks = es.getKs(); for(Keeper k : ks){ k.action(es); } } } /* 适配器 */ class KeeperAdapter extends Keeper{ @Override public void action(EventSource es) { } } class Father extends Keeper{ @Override public void action(EventSource es) { System.out.println("Father do........"); } } class Mother extends Keeper{ @Override public void action(EventSource es) { System.out.println("Monther do........"); } } class GrandFather extends Keeper{ @Override public void action(EventSource es) { System.out.println("GrandFather do........"); } } class GrandMother extends Keeper{ @Override public void action(EventSource es) { System.out.println("GrandMonther do........"); } } /* 被看护人 */ class Child implements Runnable{ EventSource es; public Child(EventSource es){ this.es = es; } public void wakeup(){ Keeper k = new KeeperAdapter(); k.receive(this.es); } @Override public void run() { System.out.println("sleep ......"); try { new Thread().sleep(5000);//5秒后醒来 this.wakeup(); } catch (InterruptedException e) { e.printStackTrace(); } } } /* 传递信息 */ class EventSource{ private String something_msg; private List<Keeper> ks; public EventSource(String something_msg, List<Keeper> ks) { super(); this.something_msg = something_msg; this.ks = ks; } public String getSomething_msg() { return something_msg; } public void setSomething_msg(String something_msg) { this.something_msg = something_msg; } public List<Keeper> getKs() { return ks; } public void setKs(List<Keeper> ks) { this.ks = ks; } } public class EventTest { public static void main(String[] args) { List<Keeper> ks = new ArrayList<Keeper>(); ks.add(new Father()); ks.add(new GrandFather()); EventSource es = new EventSource("test something",ks); Child child = new Child(es); new Thread(child).start(); } }
看上去两端代码的效果都是一样,但是前一个要比后一个要简练很多,而且设计也好很多!大家认为呢? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |