实现观察者模式设计方案

新建 侦听者类: Observer

package com.test.spring.listener;

import java.util.Map;

public interface Observer {
public void execute(Map<String, Object> paras);
public boolean isAsyn();
}

  

抽像侦听者

package com.test.spring.listener;

public abstract class AbstractObserver implements Observer {

    protected boolean asyn = false;

    public boolean isAsyn() {
        return asyn;
    }

    public void setAsyn(boolean asyn) {
        this.asyn = asyn;
    }

}

一个测试的侦听者:

package com.test.spring.listener.observers;

import java.util.Map;

import com.test.spring.listener.AbstractObserver;

public class Test1 extends AbstractObserver {

    @Override
    public void execute(Map<String, Object> paras) {
        System.out.println("i am here");
        System.out.println("thread name Observer:" + Thread.currentThread().getName());

    }

}

事件管理类: EventManager

package com.test.spring.listener;

import java.util.List;
import java.util.Map;

public class EventManager {

    private Map<String, List<Observer>> events;

    public void dispatchEvent(String eventName, Map<String, Object> paras) {
        if (events == null || events.isEmpty()) {
            return;
        }

        List<Observer> observers = events.get(eventName);
        for(Observer observer : observers) {
            if (observer.isAsyn()) {
                EventManagerThread eventManagerThread = new EventManagerThread(observer, paras);
                Thread thread = new Thread(eventManagerThread);
                thread.start();
            } else {
                observer.execute(paras);
            }
        }
    }

    public void setEvents(Map<String, List<Observer>> events) {
        this.events = events;
    }
}

class EventManagerThread implements Runnable {
    private Observer observer;
    private Map<String, Object> paras;

    public EventManagerThread(Observer observer, Map<String, Object> paras) {
        this.observer = observer;
        this.paras = paras;
    }

    @Override
    public void run() {
        this.observer.execute(paras);
    }

}

配置文件 : listener.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx

       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  <bean id="eventManager" class="com.test.spring.listener.EventManager">
    <property name="events">
        <map>
            <entry key="event1">
                <list>
                   <ref bean="test1"/>
                </list>
            </entry>
    </map>
    </property>
  </bean>

  <bean id="test1" class="com.test.spring.listener.observers.Test1">
  <property name="asyn" value="false"></property>
  </bean>

</beans>

测试类:

package com.test.spring.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] locations = {"listener.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);

        EventManager eventManager = (EventManager)ctx.getBean("eventManager");
        System.out.println("thread name main:" + Thread.currentThread().getName());
        eventManager.dispatchEvent("event1", null);
    }

}

  

posted @ 2013-12-14 15:22  森林木马  阅读(431)  评论(0编辑  收藏  举报