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

架构设计之源:设计模式的场景分析(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. The subscribe 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 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

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

-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值