现在的位置: 首页 > 综合 > 正文

spring中的设计模式Observer pattern

2012年09月09日 ⁄ 综合 ⁄ 共 3900字 ⁄ 字号 评论关闭

转载 http://rickyboy.iteye.com/blog/568605  

观察者模式在不仅在java SWT中还是spring hibernate 应用都是非常广泛的,下面就我的理解做个记录。 

首先要理解一些概念是必须的 
事件源:事件源就是一个事件发生的组件,例如按钮,面板,在spring中可以表现为容器。 

事件:我们都知道当我们点击一下按钮就是一个事件发生了。在具体表现为ActionEvent类。 
比如时钟Timer类发生定时性动作时。 

监听器:当某个事件发生的时候,监听器就会监听到,来进行相应的动作。当然一个容器有可能既是事件源。 
下面是一个spring的观察者模式的事件的例子, 
在spring中要想具有监听器功能就必须继承ApplicationListener 
事件就要实现ApplicationEvent 



下面是我实现的例子: 

配置文件bean-config.xml 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"   
  3.   "http://www.springframework.org/dtd/spring-beans.dtd">   
  4.   
  5. <beans>   
  6.  <bean id="emailer" class="com.starit.bean.EmailBean">  
  7. <property name="blackList">  
  8. <list>  
  9. <value>black@list.org</value>  
  10. <value>white@list.org</value>  
  11. <value>john@doe.org</value>  
  12. </list>  
  13. </property>  
  14. </bean>  
  15. <bean id="blackListListener" class="com.starit.bean.BlackListNotifier">  
  16. <property name="notificationAddress">  
  17. <value>spam@list.org</value>  
  18. </property>  
  19. </bean>  
  20. </beans>  


监听器类 

Java代码  收藏代码
  1. package com.starit.bean;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4. import org.springframework.context.ApplicationListener;  
  5.   
  6. public class BlackListNotifier implements ApplicationListener {     
  7.     /** notification address */    
  8.     private String notificationAddress;  
  9.     
  10.     public void setNotificationAddress(String notificationAddress) {     
  11.     this.notificationAddress = notificationAddress;     
  12.     }     
  13.     public void onApplicationEvent(ApplicationEvent event) {  
  14.         if (event instanceof BlackListEvent) {     
  15.                 System.out.println(((BlackListEvent) event).getEmail().getBlackList());  
  16.         }         
  17.     }  
  18. }  



触发事件类 

Java代码  收藏代码
  1. package com.starit.bean;  
  2. import org.springframework.context.ApplicationEvent;  
  3.   
  4. public class BlackListEvent extends ApplicationEvent{  
  5.     private static final long serialVersionUID = 1L;  
  6.     private EmailBean email = null;  
  7.     public BlackListEvent(Object o) {     
  8.         super(o);   
  9.     }    
  10.     public BlackListEvent(Object source, EmailBean object) {  
  11.         super(source);  
  12.         this.setEmail(object);  
  13.     }  
  14.     public void setEmail(EmailBean email) {  
  15.         this.email = email;  
  16.     }  
  17.     public EmailBean getEmail() {  
  18.         return email;  
  19.     }  



事件源bean类 

Java代码  收藏代码
  1. package com.starit.bean;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.ApplicationContextAware;  
  7.   
  8. public class EmailBean implements ApplicationContextAware {    
  9.       
  10.     private ApplicationContext ctx = null;  
  11.     /** the blacklist */    
  12.     private List blackList;     
  13.     public void setBlackList(List blackList){     
  14.         this.blackList = blackList;     
  15.     }     
  16.     public void setApplicationContext(ApplicationContext ctx) {     
  17.         this.ctx = ctx;     
  18.     }  
  19.     public List getBlackList() {  
  20.         return blackList;  
  21.     }  
  22.     public void sendEmail(String address, String text){      
  23.         BlackListEvent evt = new BlackListEvent(address, (EmailBean)ctx.getBean("emailer"));     
  24.         ctx.publishEvent(evt);      
  25.     }     
  26. }   



测试类如下: 

Java代码  收藏代码
  1. package com.starit.bean;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  5.   
  6. public class Test {  
  7.     public static void main(String args[]) {  
  8.         ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml");  
  9.         EmailBean emailBean = (EmailBean)context.getBean("emailer");  
  10.         emailBean.sendEmail("black@list.org","1212");  
  11.     }  
  12. }  



以上的实现的步骤如下: 
1.注册监听器。 
2.事件源容器通过ctx.publishEvent(evt);触发事件 。 
3.这时候我们的监听器就会能够监听到这个事件,就能够触发相应的动作。 
总结: 
程序中容器是事件源。也就是观察者模式中的主体对象。实现ApplicationListener接口的是监听器,也就是Observer模式中的观察者。实现ApplicationEvent是一个事件, 
ps:http://www.iteye.com/topic/1292 事件模型 
    http://www.iteye.com/topic/522171  AWT事件模型 
    http://www.iteye.com/topic/519498  很好理解spring监听器的例子 
    http://www.iteye.com/topic/102068 观察者模式剖析

抱歉!评论已关闭.