Skip to content

Commit

Permalink
feat:上传观察者模式-JDK事件驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Sep 16, 2019
1 parent 4394328 commit a5145aa
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@


1.观察者模式 - 已完成
* 常规观察者模式-已完成
* 事件驱动方式-已完成
* 使用GUAVA的EventBus方式-未完成

2.装饰模式 - 已完成

Expand Down
10 changes: 3 additions & 7 deletions src/main/com/mxy/design/observer/WeChatOfficialAccountImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ public boolean cancelFollowUser(WeChatUser weChatUser) {

@Override
public boolean notifyObServer(String sendMsg) {
articleInfo(sendMsg);
return true;
}

private void articleInfo(String articleInfo) {
Preconditions.checkArgument(StringUtils.isNotBlank(articleInfo));
Preconditions.checkArgument(StringUtils.isNotBlank(sendMsg));
users.stream().forEach(u -> {
u.update(u.getUserName() + "-已收到文章-" + articleInfo + ".");
u.update(u.getUserName() + "-已收到文章-" + sendMsg + ".");
});
return true;
}
}
24 changes: 24 additions & 0 deletions src/main/com/mxy/design/observer/event/driver/EventSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mxy.design.observer.event.driver;

import java.util.ArrayList;
import java.util.List;

public class EventSource {

public static List<ObServerListener> listeners = new ArrayList<>();

public void addListener(ObServerListener listener) {
listeners.add(listener);
}

public void removeListener(ObServerListener listener) {
listeners.remove(listener);
}

public void notifies(String key,WeChatUser user){
listeners.forEach(listener -> {
listener.handleEvent(new WeChatOfficialAccount(key,user));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mxy.design.observer.event.driver;

import java.util.EventListener;
import java.util.EventObject;

public interface ObServerListener extends EventListener {
/**
* 接受公众号推送的信息
*/
void handleEvent(EventObject event);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mxy.design.observer.event.driver;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;


public class WeChatOfficialAccount extends EventObject {

private static List<WeChatUser> users = new ArrayList<>();
private WeChatUser user;
private String source;

public WeChatUser getUser() {
return user;
}

public void setUser(WeChatUser user) {
this.user = user;
}

@Override
public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}


public WeChatOfficialAccount(String source, WeChatUser user) {
super(source);
this.source = source;
this.user = user;
}

public boolean followUser(WeChatUser weChatUser) {
System.out.println(weChatUser.getUserName() + "-->关注了该公众号.");
return users.add(weChatUser);
}

public boolean cancelFollowUser(WeChatUser weChatUser) {
System.out.println(weChatUser.getUserName() + "-->取消了关注.");
return users.remove(weChatUser);
}

public static void notifyObServer(String msg) {
Preconditions.checkArgument(StringUtils.isNotBlank(msg));
users.stream().forEach(u -> {
u.update(u.getUserName() + "-已收到文章-" + msg + ".");
});
}
}
55 changes: 55 additions & 0 deletions src/main/com/mxy/design/observer/event/driver/WeChatUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.mxy.design.observer.event.driver;


import com.google.common.base.Objects;

import java.io.Serializable;

/**
* 微信用户 实现观察接口 随时接受更新的信息
*/
public class WeChatUser implements Serializable {

private int id;
private String userName;

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) {
this.id = id;
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 user = (WeChatUser) o;
return id == user.id &&
Objects.equal(userName, user.userName);
}

@Override
public int hashCode() {
return Objects.hashCode(id, userName);
}
}
50 changes: 50 additions & 0 deletions src/test/com/mxy/design/observer/event/driver/EventDriverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mxy.design.observer.event.driver;

import org.junit.Test;

import java.util.EventObject;
import java.util.Objects;

/**
* 使用事件驱动 关键点 1.事件源 2.事件监听 3.事件对象
* 必要条件 主题(1)与观察者(n) 或者说是可观察者(1)与观察者(n)
* 着重强调 解耦思维方式
*/
public class EventDriverTest {

@Test
public void logicTest() {
EventSource source = new EventSource();
ObServerListener followUserListener = new ObServerListener() {
@Override
public void handleEvent(EventObject event) {
WeChatOfficialAccount account = (WeChatOfficialAccount) event;
if (Objects.equals(account.getSource(), "followUser")) {
account.followUser(account.getUser());
}
}
};
ObServerListener cancelFollowUserListener = new ObServerListener() {
@Override
public void handleEvent(EventObject event) {
WeChatOfficialAccount account = (WeChatOfficialAccount) event;
if (Objects.equals(account.getSource(), "cancelFollowUser")) {
account.cancelFollowUser(account.getUser());
}
}
};
source.addListener(followUserListener);
source.addListener(cancelFollowUserListener);
source.notifies("followUser", new WeChatUser(2, "王五"));
source.notifies("followUser", new WeChatUser(1, "张三"));
source.notifies("followUser", new WeChatUser(3, "李四"));

//推送文章
WeChatOfficialAccount.notifyObServer("C#学习入门");
//王五不关注了 觉得该公众号很烂 ^_^
source.notifies("cancelFollowUser", new WeChatUser(2, "王五"));
//继续推送文章给其他两位
WeChatOfficialAccount.notifyObServer("spring学习入门");
}

}

0 comments on commit a5145aa

Please sign in to comment.