-
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.
- Loading branch information
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
src/main/com/mxy/design/observer/event/guava/eventbus/WeChatOfficialAccount.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,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<WeChatUser> users = new ArrayList<>(); | ||
|
||
public static List<WeChatUser> getUsers() { | ||
return users; | ||
} | ||
|
||
public static void setUsers(List<WeChatUser> users) { | ||
WeChatOfficialAccount.users = users; | ||
} | ||
|
||
public static void notifyObServer(String msg) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(msg)); | ||
users.stream().forEach(u -> { | ||
u.update(u.getUserName() + "-已收到文章-" + msg + "."); | ||
}); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/com/mxy/design/observer/event/guava/eventbus/WeChatUser.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,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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/com/mxy/design/observer/event/guava/eventbus/EventBusTest.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,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学习入门"); | ||
} | ||
|
||
} |