moco代码赏析十

接下来看一看2012.11.7的后五次提交。
第一次提交,新引入了一个类HttpServer,由MocoHttpServer类分离出一部分方法形成了这个类。

public class HttpServer {
    private final int port;
    private List settings = new ArrayList();
    private ContentHandler anyResponseHandler;

    public HttpServer(int port) {
        this.port = port;
    }

    public Setting request(RequestMatcher matcher) {
        BaseSetting setting = new BaseSetting(matcher);
        this.settings.add(setting);
        return setting;
    }

    public Setting request(RequestMatcher... matchers) {
        return request(or(matchers));
    }

    public void response(String response) {
        responseWithContentHandler(new ContentHandler(response));
    }

    public void response(ContentStream stream) {
        responseWithContentHandler(new ContentHandler(stream.asByteArray()));
    }

    private void responseWithContentHandler(ContentHandler contentHandler) {
        this.anyResponseHandler = contentHandler;
    }

    public Setting get(RequestMatcher matcher) {
        return request(and(new GetMethodRequestMatcher(), matcher));
    }

    public Setting post(RequestMatcher matcher) {
        return request(and(new PostMethodRequestMatcher(), matcher));
    }

    public int getPort() {
        return port;
    }

    public List getSettings() {
        return settings;
    }

    public ContentHandler getAnyResponseHandler() {
        return anyResponseHandler;
    }
}

有了这次提交以后,我们就可以来聊一下moco项目的组织结构了。下面我们来看一下使用者该如何与moco项目进行交互。
未命名文件-3.png

虚线左侧是moco,右侧是使用者。使用者通过running()方法来创建一个MocoHttpServer,使用者需要传入自己的请求和一个HttpServer。那HttpServer和MocoHttpServer有怎样的关联,有什么样的区别呢?

MocoHttpServer是moco项目内部的类,诸如start一个服务器,如何与netty交互等,不需要使用者关心的方法都放在了这个类中。

而HttpServer类则是一个servlet暴露给使用者的一层,提前设定好碰到哪种请求做何种处理回复。其中的方法都是使用者需要关心的、使用的。如设定使用端口(除了端口之外,后面的代码应该会把IP地址也开放出来,现在默认是localhost)、发送request请求,设定当收到request时,该如何response。有get()和post()方法等。值得一提的是,作者会把所有暴露给使用者的方法内部不添加任何逻辑,直接内部调用该类的其他私有方法来处理。如使用者可以通过running方法来调用MocoHttpServer的start()方法,而start()方法没有任何逻辑,只做了一件事,就是调用doStart()方法,在doStart()方法中实现逻辑。这样做的好处就是:一、将真正的逻辑私有化,安全性高。二、当代码逻辑需要扩展时,可以不影响外部调用。

作者的这些做法也就告诉了我们一些coding的原则:一、在需要和外部的接口函数中,不要写入任何的逻辑,直接调用内部其他私有的方法即可,在该私有方法中实现逻辑。二、将与外部交互的方法和内部的方法分开,会使得代码更加清晰可读、易于扩展。我们再来看下一次提交,来印证这一观点。
原代码:

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        Object message = e.getMessage();

        if (message instanceof HttpRequest) {
            Channel channel = e.getChannel();

            writeResponse(channel, (HttpRequest) message);

            channel.disconnect();
            channel.close();
        }
    }

改动后:

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        Object message = e.getMessage();

        if (message instanceof HttpRequest) {
            httpRequestReceived((HttpRequest) message, e.getChannel());
        }
    }

    private void httpRequestReceived(HttpRequest request, Channel channel) {
        writeResponse(request, channel);

        channel.disconnect();
        channel.close();
    }

messageReceived()方法是需要与netty框架进行交互的方法,所以我们不应该在这个方法中加入任何的逻辑。只需要在这个方法中,调用我们的另一个私有方法来处理逻辑即可。于是有了改动后的代码,messageReceived()方法直接转发了请求到httpRequestReceived()方法中,做了处理。这也就印证了我们的观点。

你可能感兴趣的:(moco代码赏析十)