Java Event Bus 事件监听器的应用

基于事件监听处理的系统可以减少Java模块间代码的耦合度, 使代码结构更加清晰. 实现所谓的"职责分离"

比如,在做删除的时候, 把监听器注册到删除方法中,

在事件类中完成对象删除的后续处理, 比如,删除另外的表中相关的数据.  就是比较好的方式.

Java中注册监听器比较繁琐,  可以使用一个开源的EventBus 1.4 API框架来实现.

public class TestEventModel {
	private int brandPicId;
}


import org.bushe.swing.event.annotation.EventSubscriber;

public class BrandEvent {
	@EventSubscriber(eventClass = TestEventModel.class)
	public void brandDelete(TestEventModel testEventModel) {
		System.out.println("Haha");
	}

}

import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.annotation.AnnotationProcessor;

public class TestEventBus {

	public static void main(String[] args) {
		BrandEvent event = new BrandEvent();
		AnnotationProcessor.process(event);
		EventBus.publish(new TestEventModel());

	}
}

猜你喜欢

转载自timup.iteye.com/blog/2192215