• 实现观察者模式设计方案


    新建 侦听者类: 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);
        }
    
    }
    

      

  • 相关阅读:
    Fiddler 教程
    ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
    ios 生成一个动态的随机的头像/随机数的操作
    在工程中如何使用一个公用的页面
    使用手势,让键盘在点击空白处消失
    ios开发之--iOS 11适配:iOS11导航栏返回偏移
    svn 操作字母的提示
    字面量
    控制 打开和关闭远程推送通知
    常见结构体 日期 字符串的操作 很实用
  • 原文地址:https://www.cnblogs.com/owenma/p/3474377.html
Copyright © 2020-2023  润新知