Skip to content

Commit

Permalink
add custom event listener in springboot code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ipipman committed Aug 3, 2021
1 parent bf97dda commit 08ca4fa
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.springboot.source.code.analysis.event;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:20 下午
*/


// 抽象的事件广播器
public abstract class AbstractEventMulticaster implements EventMulticaster {

// 事件监听器列表
private List<WeatherListener> listenerList = new CopyOnWriteArrayList<>();

@Override
public void multicastEvent(WeatherEvent event) {
// 广播所有事件监听器
listenerList.forEach(listener -> {
doStart();
// 触发
listener.onWeatherEvent(event);
doEnd();
});
}

// 添加事件监听器
@Override
public void addListener(WeatherListener listener) {
listenerList.add(listener);
}

// 删除事件监听器
@Override
public void removeListener(WeatherListener listener) {
listenerList.remove(listener);
}


// 模版方法
abstract void doStart();

abstract void doEnd();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:04 下午
*/

// 事件广播器
public interface EventMulticaster {

// 广播事件
void multicastEvent(WeatherEvent event);

// 添加监听器
void addListener(WeatherListener listener);

// 删除监听器
void removeListener(WeatherListener listener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 2:52 下午
*/

// 下雨事件
public class RainEvent extends WeatherEvent {

@Override
public String getWeather() {
return "rain";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:00 下午
*/


// 下雨监听器
public class RainListener implements WeatherListener {

@Override
public void onWeatherEvent(WeatherEvent event) {
if (event instanceof RainEvent) {
System.out.println("rain event ..." + event.getWeather());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 2:51 下午
*/

// 下雪事件
public class SnowEvent extends WeatherEvent {

@Override
public String getWeather() {
return "snow";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:02 下午
*/

// 下雪监听器
public class SnowListener implements WeatherListener {

@Override
public void onWeatherEvent(WeatherEvent event) {
if (event instanceof SnowEvent) {
System.out.println("snow event.." + event.getWeather());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:29 下午
*/


public class Test {

public static void main(String[] args) {
// 创建事件广播器
WeatherEventMulticaster multicaster = new WeatherEventMulticaster();

// 添加事件监听器
WeatherListener listener1 = new SnowListener();
WeatherListener listener2 = new RainListener();
multicaster.addListener(listener1);
multicaster.addListener(listener2);

// 广播事件
WeatherEvent event1 = new RainEvent();
WeatherEvent event2 = new SnowEvent();
multicaster.multicastEvent(event1);
multicaster.multicastEvent(event2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 2:50 下午
*/


// 天气事件抽象类
public abstract class WeatherEvent {

// 获取当前天气的事件
public abstract String getWeather();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 3:27 下午
*/

// 天气事件广播器
public class WeatherEventMulticaster extends AbstractEventMulticaster {

@Override
public void doStart() {
System.out.println("multicaster event begin ...");
}

@Override
public void doEnd() {
System.out.println("multicaster evnet end ....");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.springboot.source.code.analysis.event;

/**
* Created by ipipman on 2021/8/3.
*
* @version V1.0
* @Package com.example.springboot.source.code.analysis.event
* @Description: (用一句话描述该文件做什么)
* @date 2021/8/3 2:58 下午
*/

// 天气监听器
public interface WeatherListener {

// 监听天气事件
void onWeatherEvent(WeatherEvent event);
}

0 comments on commit 08ca4fa

Please sign in to comment.