架構設計之源:設計模式的場景分析(1)Publish-Subscribe
架構設計之源:設計模式的場景分析(1)Publish-Subscribe
- Author: Poechant
- Blog:blog.CSDN.net/Poechant
- Email: zhongchao.ustc@gmail.com
- Date: February 24th, 2012
我在設計模式方面僅閱讀過英文原版(或影印版)的書籍,以及一些網際網路上的資料。而設計模式中有很多專有名詞,概因我不知道部分中文譯名是什麼,所以本文以英文來寫作。
1. Motivating Scenario
Have you ever encounter such a scenario: The data generatted by someone should be passed to the rest of others or some others in the program, and every receiver has a similar operation?
I think if you have some experience in software programming, there must be such scenarios. In the DP (Design Pattern) introduced in this article, the data generator or sender is called publisher and the receiver is called subscriber, just as you subscribe a RSS or a local newspaper and the postman sends those newspaper.
2. Publisher and Subscriber
Here is a Publisher interface. Thesubscribe
method
is used to register a subscriber to the publisher. Thepublish
method
is aimed at sending data to subscribers who have registered before.
// Poechant@CSDN
package com.sinosuperman.main;
public interface Publisher<E> {
public void subscribe(Subscriber<E> subscriber);
public void publish(E data);
}
Subscriber interface is as following. The methodgetPublication
is
invoked by the publisher to run the specific code implements by the subscribers.
// Poechant@CSDN
package com.sinosuperman.main;
public interface Subscriber<E> {
public void getPublication(E data);
}
The natural flow of publish-subscribe pattern
3. Example
Now we are trying to create a simple command processor, which has some basic functions as echo, quit and map.
3.1. Class implements Publisher
// Poechant@CSDN
package com.sinosuperman.main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class InputLoop implements Publisher<String> {
private List<Subscriber<String>> subscribers;
private InputLoop() {
subscribers = new ArrayList<Subscriber<String>>();
}
public static InputLoop create() {
return new InputLoop();
}
@Override
//register subscriber
public void subscribe(Subscriber<String> subscriber) {
if (!subscribers.contains(subscriber)) {
subscribers.add(subscriber);
}
}
@Override
// Notify all subscribers
public void publish(String data) {
for (Subscriber<String> sub : subscribers) {
sub.getPublication(data);
}
}
public static String getInput() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String response = "";
try {
response = br.readLine();
if (response == null) {
return "";
}
} catch (IOException e) {
}
return response;
}
public void loop() {
while (true) {
System.out.println("> ");
String s = getInput();
publish(s);
}
}
}
3.2. Classes implements Subscriber
Echo class could echo every string you input.
// Poechant@CSDN
package com.sinosuperman.main;
public class Echo implements Subscriber<String> {
private Echo() {
}
public static Echo create() {
return new Echo();
}
@Override
public void getPublication(String data) {
System.out.println("Got: " + data);
}
}
Map class could return the value according to the key you input.
// Poechant@CSDN
package com.sinosuperman.main;
public class Response implements Subscriber<String> {
private String ifthis;
private String thenthat;
private Response(String it, String tt) {
ifthis = it;
thenthat = tt;
}
public static Response create(String it, String tt) {
return new Response(it, tt);
}
@Override
public void getPublication(String data) {
if (data.equals(ifthis)) {
System.out.println(thenthat);
}
}
}
Quit class could execute the exit operation, then the program is halted.
// Poecahnt@CSDN
package com.sinosuperman.main;
public class Quit implements Subscriber<String> {
private Quit() {}
public static Quit create() {
return new Quit();
}
@Override
public void getPublication(String data) {
if (data.equals("quit")) {
System.exit(0);
}
}
}
3.3. Benchmark
Create an object of InputLoop and four subscribers.
package com.sinosuperman.main;
public class Test {
public static void main(String[] args) {
InputLoop il = InputLoop.create();
il.subscribe(Echo.create());
il.subscribe(Quit.create());
il.subscribe(Response.create("foo", "bar"));
il.subscribe(Response.create("1+1", "2"));
il.loop();
}
}
Test:
>
this is a string
Got: this is a string
>
help
Got: help
>
foo
Got: foo
bar
>
1+1
Got: 1+1
2
>
foo
Got: foo
bar
>
quit
Got: quit
-
轉載請註明來自Poechant@CSDN
-
相關文章
- React 設計模式和場景分析React設計模式
- 架構設計思想-微服務架構設計模式架構微服務設計模式
- Unity應用架構設計(1)—— MVVM 模式的設計和實施(Part 1)Unity應用架構MVVM模式
- 設計模式應用場景之Model設計中可以用到的設計模式設計模式
- Unity應用架構設計(1)—— MVVM 模式的設計和實施(Part 2)Unity應用架構MVVM模式
- Flutter 在流式場景下的架構設計與應用Flutter架構
- MapReduce1架構設計架構
- 架構師之路—理解設計模式架構設計模式
- 架構師對MVC設計模式的理解架構MVC設計模式
- thrift原始碼分析-架構設計原始碼架構
- MySQL高可用架構設計分析MySql架構
- 如何從 0 到 1 設計、構建移動分析架構架構
- 場景設計中距離感的設計
- 軟體架構設計模式大全 - vikipediaaaa架構設計模式
- 效能場景設計
- 設計模式1設計模式
- 程式碼架構設計-1.為什麼要做好程式碼架構設計架構
- Twitter推薦引擎架構設計分析架構
- RPC框架整體架構設計分析RPC框架架構
- 常用的設計架構架構
- Android設計模式——策略模式之原始碼使用場景(三)Android設計模式原始碼
- 利用WMRouter 重新架構設計業務模式架構模式
- 設計模式(06)——設計原則(1)設計模式
- FMEA在架構設計中的應用分析架構
- 圖解設計模式:身份認證場景的應用圖解設計模式
- Linkedlist的應用場景:設計佇列、設計棧佇列
- 1/24 設計模式之策略設計模式 Strategy Pattern設計模式
- 金融行業聯機交易業務場景下的儲存架構設計行業架構
- 【架構師視角系列】風控場景下配置中心的設計實戰架構
- 架構設計之架構的演變架構
- 設計模式 #1(7大設計原則)設計模式
- 初探Tomcat的架構設計Tomcat架構
- 架構設計的本質架構
- UI架構設計的演化UI架構
- 理解Underscore的設計架構架構
- 流程引擎的架構設計架構
- 微服務架構和設計模式 - DZone微服務微服務架構設計模式
- Angular應用架構設計-2:Data Service模式Angular應用架構模式
- Java設計模式-策略模式分析Java設計模式