Salah Eddine Taouririt
Salah Eddine Taouririt

Reputation: 26405

What's the benefits from this kind of violation of the single responsibility principle

This code has been taken from org.glassfish.jersey.grizzly2 project, like the method name indicate, createHttpServer should be responsible "only" for creating and returning an instance of the HttpServer class, I just wonder why should the HttpServer.start call be encapsulated in such way ?

public static HttpServer createHttpServer(final URI uri,
                                              final GrizzlyHttpContainer handler,
                                              final boolean secure,
                                              final SSLEngineConfigurator sslEngineConfigurator
                                              final boolean start) {

        final String host = (uri.getHost() == null) ? NetworkListener.DEFAULT_NETWORK_HOST : uri.getHost();
        final int port = (uri.getPort() == -1) ? DEFAULT_HTTP_PORT : uri.getPort();

        final NetworkListener listener = new NetworkListener("grizzly", host, port);
        listener.setSecure(secure);
        if (sslEngineConfigurator != null) {
            listener.setSSLEngineConfig(sslEngineConfigurator);
        }

        final HttpServer server = new HttpServer();
        server.addListener(listener);

        // Map the path to the processor.
        final ServerConfiguration config = server.getServerConfiguration();
        if (handler != null) {
            config.addHttpHandler(handler, uri.getPath());
        }

        config.setPassTraceRequest(true);

        if (start) {
            try {
                // Start the server.
                server.start();
            } catch (IOException ex) {
                throw new ProcessingException(LocalizationMessages.FAILED_TO_START_SERVER(ex.getMessage()), ex);
            }
        }

        return server;
    }

Upvotes: 0

Views: 94

Answers (2)

Tanmay Patil
Tanmay Patil

Reputation: 7057

This is a public API method, not a class.

Single responsibility principle in wiki says

Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class.

SRP is intended for loose coupling and robustness. It definitely helps developers in maintaining the same while keeping it well functioning.

So had it been some internal method or class, I would have agreed.


The design goals of public API are completely different.

  • First thing you have to make sure about is ease of use.
  • Your software should hide internal idiosyncrasies of the implementation and design as well.
  • If a user is calling this method, and is unaware of requirement to call other method for starting, he/she'd be confused. We can not force users to know entire workflow of the software i.e. calling each small step manually.

Hope this helps.

Upvotes: 1

excalibur1491
excalibur1491

Reputation: 1221

The only advantage I see is that the user has to write less code. I totally disagree with this practice. If it says "create", then it should only create. Anyway, as far as its clearly specified in the documntation, it shoul be "ok" to do that... It's not the worst violation of the SRP I have seen...

Upvotes: 0

Related Questions