事件通知模板
事件通知与观察者模式类似,参照《Java软件体系结构设计模式标准指南》P258,代码https://github.com/birdstudiocn/design-patterns-example/tree/master/src/38
事件接口
public interface Listener<T, S> { void listen(T event, S result); }
事件容器类
public class OrderedComposite<S> { private List<S> unordered = new ArrayList<S>(); private List<S> ordered = new ArrayList<S>(); private Comparator<? super S> comparator = new AnnotationAwareOrderComparator(); private List<S> list = new ArrayList<S>(); /** * Public setter for the listeners. * * @param items */ public void setItems(List<? extends S> items) { unordered.clear(); ordered.clear(); for (S s : items) { add(s); } } /** * Register additional item. * * @param item */ public void add(S item) { if (item instanceof Ordered) { if (!ordered.contains(item)) { ordered.add(item); } } else if (AnnotationUtils.isAnnotationDeclaredLocally(Order.class, item.getClass())) { if (!ordered.contains(item)) { ordered.add(item); } } else if (!unordered.contains(item)) { unordered.add(item); } Collections.sort(ordered, comparator); list.clear(); list.addAll(ordered); list.addAll(unordered); } /** * Public getter for the list of items. The {@link Ordered} items come * first, followed by any unordered ones. * * @return an iterator over the list of items */ public Iterator<S> iterator() { return new ArrayList<S>(list).iterator(); } /** * Public getter for the list of items in reverse. The {@link Ordered} items * come last, after any unordered ones. * * @return an iterator over the list of items */ public Iterator<S> reverse() { ArrayList<S> result = new ArrayList<S>(list); Collections.reverse(result); return result.iterator(); } }
事件注册类
public class CompositeListener<T, S> implements Listener<T, S> { private OrderedComposite<ProcessListener<? super T, ? super S>> listeners = new OrderedComposite<ProcessListener<? super T, ? super S>>(); /** * Public setter for the listeners. * * @param itemReadListeners */ public void setListeners(List<? extends Listener<? super T, ? super S>> listeners) { this.listeners.setItems(listeners); } /** * Register additional listener. * * @param itemReaderListener */ public void register(ProcessListener<? super T, ? super S> listener) { listeners.add(listener); } /** * Call the registered listeners in reverse order, respecting and * prioritising those that implement {@link Ordered}. * * @see org.springframework.batch.core.ProcessListener#afterProcess(java.lang.Object, * java.lang.Object) */ @Override public void listen(T item, S result) { for (Iterator<Listener<? super T, ? super S>> iterator = listeners.reverse(); iterator.hasNext();) { Listener<? super T, ? super S> listener = iterator.next(); listener.listen(item, result); } } }
事件目标实现类,Observable
public class ConcreteListenSubject { private CompositeListener<BaseEvent<VO, ResultVO>, BaseResult> listener = new CompositeListener<BaseEvent<VO, ResultVO>, BaseResult>(); public void registerListener( ProcessListener<BaseProcessEvent<VO, ResultVO>, BaseResult> listener) { this.listener.register(listener); } public ResultVO process(VO vo) throws Exception { BaseEvent<VO, ResultVO> event = new BaseEvent<VO, ResultVO>(vo,resultVO); listener.afterProcess(event, result); return resultVO; } }
事件观察实现,Observer
ConcreteListenSubject subject=new ConcreteListenSubject (); subject.registerListener(new Listener() { @Override public void listen(Object event, Object result) { // TODO Auto-generated method stub } });