一、场景假设
假设有博客系统中需要实现如下功能:
系统中用户发布文章,修改文章,删除文章时,需要一些相关的操作需要执行。
发布文章后,给好友发送邮件通知,给用户加积分。
修改文章后,给好友发送邮件通知,给用户加积分。
删除文章后,给好友发送邮件通知,给用户减少积分。
二、相关的概念解析
分析如上场景,可以采用事件分发和监听机制来实现。
事件分发和监听有如下几个概念:
事件源:触发事件的对象 如上场景中我们把实体对象作为事件源,发布的文章就是事件源
事件: 对事件源的操作产生事件,例如 发表文章修改文章,删除文章这些操作就会触发相关的文章被发表事件,文章被 删除事件,文章被修改事件
事件监听器:对事件源各种事件触发执行行为的抽象,包括接口和若干实现类。
比如: 接口需要定义事件源相关事件触发时需要实现的操作接口。
事件分发器:事件分发器主要处理事件的分发和事件监听器的管理,注册和删除事件监听器等。
package com.software;
/**
* @description 文章实体类
* @author TianQiSen
* @date 2017年4月8日
*/
public class ArticleEntry {
String id;
String name;
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setId(final String id) {
this.id = id;
}
public void setName(final String name) {
this.name = name;
}
}
----------
package com.software;
/**
* @description 文章管理类
* @author TianQiSen
* @date 2017年4月8日
*/
public class ArticleBiz extends EventDispatcher{
public boolean add(final ArticleEntry ae){
System.out.println("发表博客成功!");
Listener ls = new EmailListener();
addEventListener(ls);
ls = new ScoreListener();
addEventListener(ls);
dispatchEvent(new Event(Event.ARTICAL_ADDED, ae, null));
return false;
}
public boolean alter(final ArticleEntry ae){
System.out.println("博客修改成功!");
Listener ls = new EmailListener();
addEventListener(ls);
ls = new ScoreListener();
addEventListener(ls);
dispatchEvent(new Event(Event.ARTICAL_ADDED, ae, null));
return true;
}
public boolean delete(final ArticleEntry ae){
System.out.println("博客删除成功!");
Listener ls = new EmailListener();
addEventListener(ls);
ls = new ScoreListener();
addEventListener(ls);
dispatchEvent(new Event(Event.ARTICAL_ADDED, ae, null));
return true;
}
}
----------
package com.software;
import java.util.ArrayList;
import java.util.List;
/**
* @description 事件分发器,其功能包括注册事件监听器,移除事件监听器,派发事件
* @author TianQiSen
* @date 2017年4月8日
*/
public class EventDispatcher {
private static final List<Listener> list = new ArrayList<Listener>();
public static boolean addEventListener(final Listener ls){
list.add(ls);
return true;
}
public static boolean dispatchEvent(final Event e){
final int eventType = e.getEventType();
for(final Listener ls:list){
switch(eventType){
case Event.ARTICAL_ADDED:
ls.addArticle(e);
break;
case Event.ARTICAL_ALTER:
ls.alterArticle(e);
break;
case Event.ARTICAL_DELETE:
ls.deleteArticle(e);
break;
}
}
return true;
}
public static boolean removeEventListener(final Listener ls){
list.remove(ls);
return true;
}
}
----------
package com.software;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
/**
* @description 事件
* @author TianQiSen
* @date 2017年4月8日
*/
public class Event {
public static final int ARTICAL_ADDED = 101;
public static final int ARTICAL_ALTER = 102;
public static final int ARTICAL_DELETE = 103;
private final ArticleEntry ae;
private final Date date;
private final int eventType;
/** 事件辅助参数 */
private final Map<?, ?> params;
public Event(final int eventType, final ArticleEntry ae, final Map<?, ?> params) {
this.eventType = eventType;
this.ae = ae;
this.params = params != null ? Collections.unmodifiableMap(params):null;
this.date = new Date();
}
public ArticleEntry getAe() {
return ae;
}
public Date getDate() {
return date;
}
public int getEventType() {
return eventType;
}
public Map<?, ?> getMap() {
return params;
}
}
----------
package com.software;
/**
* @description 监听器接口
* @author TianQiSen
* @date 2017年4月8日
*/
public interface Listener {
boolean addArticle(Event ae);
boolean alterArticle(Event ae);
boolean deleteArticle(Event ae);
}
----------
package com.software;
/**
* @description 邮件监听器,当接到分发器发出的事件后,作出发邮件的动作
* @author TianQiSen
* @date 2017年4月8日
*/
public class EmailListener implements Listener {
@Override
public boolean addArticle(final Event ae) {
if(101 == ae.getEventType()){
System.out.println("邮件:博客发表成功!");
return true;
}
return false;
}
@Override
public boolean alterArticle(final Event ae) {
if(102 == ae.getEventType()){
System.out.println("邮件:博客修改成功!");
return true;
}
return false;
}
@Override
public boolean deleteArticle(final Event ae) {
if(103 == ae.getEventType()){
System.out.println("邮件:博客删除成功!");
return true;
}
return false;
}
}
----------
package com.software;
/**
* @description 积分分发器,当接到事件分发器发出的事件后,作出对积分处理的动作
* @author TianQiSen
* @date 2017年4月8日
*/
public class ScoreListener implements Listener {
@Override
public boolean addArticle(final Event ae) {
if(101 == ae.getEventType()){
System.out.println("积分:恭喜你增加1个积分!");
return true;
}
return false;
}
@Override
public boolean alterArticle(final Event ae) {
if(102 == ae.getEventType()){
System.out.println("积分:恭喜你增加1个积分!");
return true;
}
return false;
}
@Override
public boolean deleteArticle(final Event ae) {
if(103 == ae.getEventType()){
System.out.println("积分:不好意思减少1积分");
return true;
}
return false;
}
}
----------
package com.software;
import org.junit.Test;
/**
* @description 测试类
* @author TianQiSen
* @date 2017年4月8日
*/
public class ArticleBizTest {
ArticleBiz article = new ArticleBiz();
@Test
public void testAdd() {
final ArticleEntry ae = new ArticleEntry();
ae.setId("1");
ae.setName("圣旨");
article.add(ae);
}
@Test
public void testAlter() {
final ArticleEntry ae = new ArticleEntry();
ae.setId("1");
ae.setName("圣旨");
article.alter(ae);
}
@Test
public void testDelete() {
final ArticleEntry ae = new ArticleEntry();
ae.setId("1");
ae.setName("圣旨");
article.delete(ae);
}
}
----------