监听器在java应用中比较广泛,记得web启动的时候的监听器吗,记得spring配置中的监听器吗?,记得消息传输中的监听器吗?
没错,这就是listener的应用,无处不在,身为一个java程序员,你如果不懂的监听器的原理是不是很可悲啊,没错我感觉挺可悲的(因为我刚刚才了解到监听器的工作原理),
为了纪念自己取得的一点小小的success,写篇博文记录一下自己的一些不成熟的意见和理解
监听器,就我理解就和我们日常生活中的原始概念一样,就是监听事物的一些行为。举个例子,抗战时期,发现一个国民党特务,报告政府,政府就会派出我们的超级007特工
一路跟踪特务,一发现特务有什么异常举动,立刻报告上级,采取相应的行动。
这里面特务就是事件源
事件源代码:
package com.panther.dong.listener; /** * event type(event source) * Created by panther on 15-8-10. */ public class SpyEvent { private String name; //特务姓名 private int age; //特务年龄 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
发现特务组织有两个,一个女特务,一个男特务,所以我们的国安哥哥要辛苦一下,分成两组,分别监听两个组织,一有风吹草动立刻抓起特务,防止破坏
监听任务:
package com.panther.dong.listener; /** * event listener interface * Created by panther on 15-8-10. */ public interface EventListener { //event deal callback public void eventDispatched(SpyEvent spyEvent); }
package com.panther.dong.listener; /** * event listener base class(event driven) * Created by panther on 15-8-10. */ public abstract class SpyEventListener implements EventListener { public abstract void spy(String name, int age); @Override public void eventDispatched(SpyEvent spyEvent) { spy(spyEvent.getName(), spyEvent.getAge()); } }
监听女特务(派出女国安):
package com.panther.dong.listener; /** * listener son class to listened event have executed action * Created by panther on 15-8-10. */ public class WomenSpyEventListener extends SpyEventListener { @Override public void spy(String name, int age) { System.out.println("catch Women spy: name=" + name + ",age=" + age); } }
监听男特务(派出男国安)
package com.panther.dong.listener; /** * listener son class to listened event have executed action * Created by panther on 15-8-10. */ public class ManSpyEventListener extends SpyEventListener { @Override public void spy(String name, int age) { System.out.println("catch Man spy: name = " + name + ",age=" + age); } }
上级下了命令,特务一搞破坏就回报上级,采取抓扑行动,上级下命令:
package com.panther.dong.listener; import java.util.ArrayList; import java.util.List; /** * client method,define a listener list ,execute operate * Created by panther on 15-8-10. */ public class NationalSecurity { private List<SpyEventListener> listener = new ArrayList<SpyEventListener>(); public void addPersonEventListener(SpyEventListener spyEventListener) { this.listener.add(spyEventListener); } public void execute(String name, int age) { for (SpyEventListener spyEventListener : listener) { SpyEvent event = new SpyEvent(); event.setName("have dealed: " + name); event.setAge(age); spyEventListener.eventDispatched(event); } } }
只要execute执行,立马抓扑特务,上级的命令!!!!
上级下命令了,实施抓捕行动:
package com.panther.dong.listener; /** * Created by panther on 15-8-10. */ public class Main { public static void main(String[] args) { NationalSecurity nationalSecurity = new NationalSecurity(); // add two listener nationalSecurity.addPersonEventListener(new WomenSpyEventListener()); nationalSecurity.addPersonEventListener(new ManSpyEventListener()); //execute call,satisfy condition notify listener nationalSecurity.execute("name", 20); } }
最后,两名特务被抓捕!!国安胜利~~~
运行结果:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 10:45:46