forked from ipipman/JavaSpringBootSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from ipipman:master #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
187 changes: 187 additions & 0 deletions
springboot-source-code-analysis/(5)监听器设计模式.md
This file contains hidden or 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,187 @@ | ||
| ## 监听器 | ||
|
|
||
| ### 1、监听器模式介绍 | ||
|
|
||
| <img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3lkgk29nj31l60l0ju9.jpg" alt="image-20210803144505140" align="left" width="800" /> | ||
|
|
||
| - 系统消息由广播器(Multicaster)发布出去,发布的内容统称为事件(Event),对关键事件产生订阅的是监听器(Listener); | ||
|
|
||
|
|
||
|
|
||
| ### 2、自定义事件-创建事件 | ||
|
|
||
| - 创建天气事件抽象类 | ||
|
|
||
| ```java | ||
| // 天气事件抽象类 | ||
| public abstract class WeatherEvent { | ||
| // 获取当前天气的事件 | ||
| public abstract String getWeather(); | ||
| } | ||
| ``` | ||
|
|
||
| - 创建下雨事件实现类 | ||
|
|
||
| ```java | ||
| // 下雨事件 | ||
| public class RainEvent extends WeatherEvent { | ||
| @Override | ||
| public String getWeather() { | ||
| return "rain"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - 创建下雪事件实现类 | ||
|
|
||
| ```java | ||
| // 下雪事件 | ||
| public class SnowEvent extends WeatherEvent { | ||
| @Override | ||
| public String getWeather() { | ||
| return "snow"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| #### 3、自定义事件监听器-创建监听器 | ||
|
|
||
| - 创建天气事件监听器接口类 | ||
|
|
||
| ```java | ||
| // 天气监听器 | ||
| public interface WeatherListener { | ||
| // 监听天气事件 | ||
| void onWeatherEvent(WeatherEvent event); | ||
| } | ||
| ``` | ||
|
|
||
| - 创建下雨事件监听器实现类 | ||
|
|
||
| ```java | ||
| // 下雨监听器 | ||
| public class RainListener implements WeatherListener { | ||
| @Override | ||
| public void onWeatherEvent(WeatherEvent event) { | ||
| if (event instanceof RainEvent) { | ||
| System.out.println("rain event ..." + event.getWeather()); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| - 创建下雪事件监听器实现类 | ||
|
|
||
| ```java | ||
| // 下雪监听器 | ||
| public class SnowListener implements WeatherListener { | ||
| @Override | ||
| public void onWeatherEvent(WeatherEvent event) { | ||
| if (event instanceof SnowEvent) { | ||
| System.out.println("snow event.." + event.getWeather()); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| #### 4、自定义事件广播器-创建事件广播器 | ||
|
|
||
| - 创建事件广播器接口 | ||
|
|
||
| ```java | ||
| // 事件广播器 | ||
| public interface EventMulticaster { | ||
| // 广播事件 | ||
| void multicastEvent(WeatherEvent event); | ||
| // 添加监听器 | ||
| void addListener(WeatherListener listener); | ||
| // 删除监听器 | ||
| void removeListener(WeatherListener listener); | ||
| } | ||
|
|
||
| - 创建事件广播器的抽象类 | ||
|
|
||
| ```java | ||
|
|
||
| // 抽象的事件广播器 | ||
| 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(); | ||
| } | ||
| ``` | ||
|
|
||
| - 创建事件广播器的实现类 | ||
|
|
||
| ```java | ||
| // 天气事件广播器 | ||
| 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 ...."); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| #### 5、测试 | ||
|
|
||
| ```java | ||
| 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); | ||
| } | ||
| } | ||
| ``` | ||
|
|
50 changes: 50 additions & 0 deletions
...main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java
This file contains hidden or 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
...sis/src/main/java/com/example/springboot/source/code/analysis/event/EventMulticaster.java
This file contains hidden or 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
...e-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainEvent.java
This file contains hidden or 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
...nalysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java
This file contains hidden or 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
...e-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowEvent.java
This file contains hidden or 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
...nalysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java
This file contains hidden or 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
...e-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/Test.java
This file contains hidden or 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); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.