From f72be5c269debbc3d89ed3f56ff89aa12329a7c2 Mon Sep 17 00:00:00 2001
From: maxy19 <315802767@qq.com>
Date: Mon, 16 Sep 2019 17:13:21 +0800
Subject: [PATCH] =?UTF-8?q?feat:=E4=BD=BF=E7=94=A8guava=20eventBus=20?=
=?UTF-8?q?=E5=A4=84=E7=90=86=E7=94=9F=E4=BA=A7=E6=B6=88=E8=B4=B9=E9=97=AE?=
=?UTF-8?q?=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../guava/eventbus/WeChatOfficialAccount.java | 29 +++++++++
.../event/guava/eventbus/WeChatUser.java | 65 +++++++++++++++++++
.../event/guava/eventbus/EventBusTest.java | 45 +++++++++++++
4 files changed, 140 insertions(+), 1 deletion(-)
create mode 100644 src/main/com/mxy/design/observer/event/guava/eventbus/WeChatOfficialAccount.java
create mode 100644 src/main/com/mxy/design/observer/event/guava/eventbus/WeChatUser.java
create mode 100644 src/test/com/mxy/design/observer/event/guava/eventbus/EventBusTest.java
diff --git a/pom.xml b/pom.xml
index db8e35c..4a97004 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,7 +18,7 @@
1.8
1.8
1.7.21
- 18.0
+ 23.0
diff --git a/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatOfficialAccount.java b/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatOfficialAccount.java
new file mode 100644
index 0000000..4b029ca
--- /dev/null
+++ b/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatOfficialAccount.java
@@ -0,0 +1,29 @@
+package com.mxy.design.observer.event.guava.eventbus;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class WeChatOfficialAccount {
+
+ private static List users = new ArrayList<>();
+
+ public static List getUsers() {
+ return users;
+ }
+
+ public static void setUsers(List users) {
+ WeChatOfficialAccount.users = users;
+ }
+
+ public static void notifyObServer(String msg) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(msg));
+ users.stream().forEach(u -> {
+ u.update(u.getUserName() + "-已收到文章-" + msg + ".");
+ });
+ }
+
+}
diff --git a/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatUser.java b/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatUser.java
new file mode 100644
index 0000000..887c107
--- /dev/null
+++ b/src/main/com/mxy/design/observer/event/guava/eventbus/WeChatUser.java
@@ -0,0 +1,65 @@
+package com.mxy.design.observer.event.guava.eventbus;
+
+
+import com.google.common.base.Objects;
+
+import java.io.Serializable;
+
+/**
+ * 微信用户 实现观察接口 随时接受更新的信息
+ */
+public class WeChatUser implements Serializable {
+
+ private int id;
+ private String userName;
+ private String type;
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public WeChatUser(int id, String userName,String type) {
+ this.id = id;
+ this.type = type;
+ this.userName = userName;
+ }
+
+
+ public void update(String receiveMsg) {
+ System.out.println(receiveMsg);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ WeChatUser that = (WeChatUser) o;
+ return id == that.id &&
+ Objects.equal(userName, that.userName);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(id, userName);
+ }
+}
diff --git a/src/test/com/mxy/design/observer/event/guava/eventbus/EventBusTest.java b/src/test/com/mxy/design/observer/event/guava/eventbus/EventBusTest.java
new file mode 100644
index 0000000..1f7f4cf
--- /dev/null
+++ b/src/test/com/mxy/design/observer/event/guava/eventbus/EventBusTest.java
@@ -0,0 +1,45 @@
+package com.mxy.design.observer.event.guava.eventbus;
+
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import org.junit.Test;
+
+import java.util.Objects;
+
+public class EventBusTest {
+
+ @Test
+ public void logicTest() {
+ EventBus eventBus = new EventBus();
+ eventBus.register(new Object() {
+ @Subscribe
+ public void followUser(WeChatUser weChatUser) {
+ if (!Objects.equals(weChatUser.getType(), "followUser")) {
+ return;
+ }
+ System.out.println(weChatUser.getUserName() + "-->关注了该公众号.");
+ WeChatOfficialAccount.getUsers().add(weChatUser);
+ }
+
+ @Subscribe
+ public void cancelFollowUser(WeChatUser weChatUser) {
+ if (!Objects.equals(weChatUser.getType(), "cancelFollowUser")) {
+ return;
+ }
+ System.out.println(weChatUser.getUserName() + "-->取消了关注.");
+ WeChatOfficialAccount.getUsers().remove(weChatUser);
+ }
+ });
+ eventBus.post(new WeChatUser(1, "张三", "followUser"));
+ eventBus.post(new WeChatUser(2, "李四", "followUser"));
+ eventBus.post(new WeChatUser(3, "王五", "followUser"));
+
+ WeChatOfficialAccount.notifyObServer("C#学习入门");
+
+ //王五不关注了 觉得该公众号很烂 ^_^
+ eventBus.post(new WeChatUser(3, "王五","cancelFollowUser"));
+ //继续推送文章给其他两位
+ WeChatOfficialAccount.notifyObServer("spring学习入门");
+ }
+
+}