Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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
pull merged 3 commits into Mu-L:master from ipipman:master
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,8 @@ public ConfigurableApplicationContext run(String... args) {





#### 5、推荐使用的系统初始化器【调用机制】

<img src="/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210803143602922.png" alt="image-20210803143602922" align="left" width="800" />
187 changes: 187 additions & 0 deletions springboot-source-code-analysis/(5)监听器设计模式.md
View file Open in desktop
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);
}
}
```

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);
}

}
Loading

AltStyle によって変換されたページ (->オリジナル) /