您的位置:首页 > 运维架构 > 网站架构

架构设计之源:设计模式的场景分析(1)Publish-Subscribe

2012-02-25 08:38 686 查看



架构设计之源:设计模式的场景分析(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. The
subscribe
method
is used to register a subscriber to the publisher. The
publish
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 method
getPublication
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



Publish-Subscribe Design 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

-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: