有独立派遣模块的事件系统用java编写的例子
时间: 2024-05-08 16:18:29 浏览: 13
好的,我理解您的要求并且会保密您说过的话。关于您的问题,您可以参考以下的Java代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class EventSystem {
private static EventSystem instance;
private List<Event> events;
private EventSystem() {
events = new ArrayList<>();
}
public static EventSystem getInstance() {
if (instance == null) {
instance = new EventSystem();
}
return instance;
}
public void addEvent(Event event) {
events.add(event);
}
public void removeEvent(Event event) {
events.remove(event);
}
public void dispatchEvent(Event event) {
for (Event e : events) {
if (e.getType().equals(event.getType())) {
e.onEvent(event);
}
}
}
}
interface EventListener {
void onEvent(Event event);
}
class Event {
private String type;
public Event(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
class DispatchEvent extends Event {
private String message;
public DispatchEvent(String message) {
super("dispatch");
this.message = message;
}
public String getMessage() {
return message;
}
}
class LoginEventListener implements EventListener {
@Override
public void onEvent(Event event) {
if (event instanceof DispatchEvent) {
DispatchEvent dispatchEvent = (DispatchEvent) event;
System.out.println("User logged in with message: " + dispatchEvent.getMessage());
}
}
}
class LogoutEventListener implements EventListener {
@Override
public void onEvent(Event event) {
if (event instanceof DispatchEvent) {
DispatchEvent dispatchEvent = (DispatchEvent) event;
System.out.println("User logged out with message: " + dispatchEvent.getMessage());
}
}
}
class Main {
public static void main(String[] args) {
EventSystem eventSystem = EventSystem.getInstance();
eventSystem.addEvent(new DispatchEvent("Hello World!"));
eventSystem.addEventListener(new LoginEventListener());
eventSystem.addEventListener(new LogoutEventListener());
eventSystem.dispatchEvent(new DispatchEvent("User logged in"));
eventSystem.dispatchEvent(new DispatchEvent("User logged out"));
eventSystem.removeEvent(new DispatchEvent("Hello World!"));
eventSystem.dispatchEvent(new DispatchEvent("User logged in"));
eventSystem.dispatchEvent(new DispatchEvent("User logged out"));
}
}
```
该示例代码实现了一个简单的事件系统,使用Java编写,其中包括了独立的派遣模块。在Main类中,添加了一个DispatchEvent事件,并注册了LoginEventListener和LogoutEventListener事件监听器。在派送事件时,会根据事件类型派发到对应的事件监听器中进行处理。同时,也提供了添加和删除事件的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)