架構設計之源:設計模式的場景分析(1)Publish-Subscribe

鍾超發表於2012-02-25

架構設計之源:設計模式的場景分析(1)Publish-Subscribe

我在設計模式方面僅閱讀過英文原版(或影印版)的書籍,以及一些網際網路上的資料。而設計模式中有很多專有名詞,概因我不知道部分中文譯名是什麼,所以本文以英文來寫作。

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. Thesubscribemethod is used to register a subscriber to the publisher. Thepublishmethod 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 methodgetPublicationis 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

Resize icon

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

-

相關文章