我们都很熟悉微信的公众号了,所以我们用一个专门发八卦新闻的微信公众号作为需求例子
需求故事
- 作为UserA,我希望GossipNewsPublisher可以register我,然后send “Gossip News 1”消息给我,这样UserA就可以read消息"Gossip News 1".
- 作为UserB,我希望GossipNewsPublisher可以register我,然后send “Gossip News 2”消息给我,这个UserB可以 watch 消息"Gossip News 2"同时UserA也可以read消息"Gossip News 2"
- 作为已经注册的UserA,我希望GossipNewsPublisher可以remove我,这样如果GossipNewsPublisher send “Gossip News 3”消息,UserA 将会read消息"Gossip News 2", 但是UserB将会watch消息"Gossip News 3"
Story1
Story1 Test Case
@Test
public void testSendGrossipNews(){
GossipPublisher gossipPublisher = new Gossippublisher();
UserA userA = new UserA();
//Story 1
gossipPublisher.register(userA);
gossipPublisher.send("Gossip News 1");
assertEquals("Gossip News 1", userA.read());
}
Story1 Implementation
由于这时候只有一个订阅者,那么只考虑简单的实现方式
public class GossipPublisher {
private UserA registeredUser;
public void register(UserA userA) {
registeredUser= userA;
}
public void send(String news) {
registeredUser.update(news);
}
}
public class UserA {
private String recivedNews = "";
public String read() {
return recivedNews;
}
public void update(String news) {
recivedNews = news;
}
}
运行测试,测试顺利通过
Story2
Story2 Test Case
@Test
public void testSendGrossipNews(){
GossipPublisher gossipPublisher = new GossipPublisher();
UserA userA = new UserA();
//Story 1
gossipPublisher.register(userA);
gossipPublisher.send("Gossip News 1");
assertEquals("Gossip News 1", userA.read());
//Story 2
UserB userB = new UserB();
gossipPublisher.register(userB);
gossipPublisher.send("Gossip News 2");
assertEquals("Gossip News 2",userB.watch());
assertEquals("Gossip News 2",userA.read());
}
Story2 Implementation
由于Story2就可以直接进化到Observer模式了,而java本身已经自带了Observer的相关接口,所以就直接使用Observer和Observable来实现了:
public class GossipPublisher extends Observable {
public void register(Observer observer) {
super.addObserver(observer);
}
public void send(String news) {
super.setChanged();
super.notifyObservers(news);
}
}
public class UserA implements Observer{
private String readNews = "";
public String read() {
return readNews;
}
@Override
public void update(Observable o, Object arg) {
readNews = String.valueOf(arg);
}
}
public class UserB implements Observer {
private String watchedNews = "";
public String watch() {
return watchedNews;
}
@Override
public void update(Observable o, Object arg) {
watchedNews=String.valueOf(arg);
}
}
从上面的简单代码看到Observer本质其实就是把UserA和UserB的共同行为update抽象成一个接口,然后再把GossipPublisher中针对Observer接口的操作进行抽取放在Observable中,这样通过让八卦公众号发布者继承Observable,让用户实现Observer接口,就可以很轻松的解决这种消息订阅和消息分发的问题了。
Story 3 Test Case
public class GossipPublisherTest {
public static final String GOSSIP_NEWS_1 = "Gossip News 1";
public static final String GOSSIP_NEWS_2 = "Gossip News 2";
public static final String GOSSIP_NEWS_3 = "Gossip News 3";
@Test
public void testSendGrossipNews() {
GossipPublisher gossipPublisher = new GossipPublisher();
UserA userA = new UserA();
//Story 1
gossipPublisher.register(userA);
gossipPublisher.send(GOSSIP_NEWS_1);
assertEquals(GOSSIP_NEWS_1, userA.read());
//Story 2
UserB userB = new UserB();
gossipPublisher.register(userB);
gossipPublisher.send(GOSSIP_NEWS_2);
assertEquals(GOSSIP_NEWS_2, userB.watch());
assertEquals(GOSSIP_NEWS_2, userA.read());
//Story 3
gossipPublisher.unregister(userA);
gossipPublisher.send(GOSSIP_NEWS_3);
assertEquals(GOSSIP_NEWS_2, userA.read());
assertEquals(GOSSIP_NEWS_3, userB.watch());
}
}
Story 3 Implementation
由于story3主要是要测试一下注销的功能,所以依然使用Observable里面的deletObserver功能来实现,顺利通过测试。
public class GossipPublisher extends Observable {
public void register(Observer observer) {
super.addObserver(observer);
}
public void send(String news) {
super.setChanged();
super.notifyObservers(news);
}
public void unregister(UserA userA) {
super.deleteObserver(userA);
}
}
和坚思辨
Observer模式所解决的问题其实在日常的开发中很常见,因为它的本质是解决发送消息通知的问题。
- 比如在UI界面上对一个组件进行操作以后,其他几个组件要发生相应的改变
- 比如在完成一个数据库的持久化以后,需要通知其他的缓存模块进行同步
因此只要是遇到这种1对多的消息传输问题,尤其是多还是可变的时候,就应该立刻想到应该使用Observer模式了