forked from ipipman/JavaSpringBootSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom event listener in springboot code analysis
- Loading branch information
Showing
10 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
23 changes: 23 additions & 0 deletions
23
...sis/src/main/java/com/example/springboot/source/code/analysis/event/EventMulticaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
19 changes: 19 additions & 0 deletions
19
...e-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...nalysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...e-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...nalysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...e-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...nalysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
24 changes: 24 additions & 0 deletions
24
.../main/java/com/example/springboot/source/code/analysis/event/WeatherEventMulticaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ...."); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |