-
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
7 changed files
with
205 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
|
||
|
||
1.观察者模式 - 已完成 | ||
* 常规观察者模式-已完成 | ||
* 事件驱动方式-已完成 | ||
* 使用GUAVA的EventBus方式-未完成 | ||
|
||
2.装饰模式 - 已完成 | ||
|
||
|
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
24 changes: 24 additions & 0 deletions
24
src/main/com/mxy/design/observer/event/driver/EventSource.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,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)); | ||
}); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/com/mxy/design/observer/event/driver/ObServerListener.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,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); | ||
|
||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/com/mxy/design/observer/event/driver/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,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
55
src/main/com/mxy/design/observer/event/driver/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,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
50
src/test/com/mxy/design/observer/event/driver/EventDriverTest.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,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学习入门"); | ||
} | ||
|
||
} |