-1
\$\begingroup\$

I have Java (or C#, Python, Kotlin, or other similar language) class that will be used to communicate with client over network. Protocol used by this application allows many different ways to create connection (TCP, unix socket, serial port, …) and perform handshake (plain connection or TLS, with or without authentication). I use separate abstract class that will initialize the connection (called ConnectionStarter) and create instance of Connection. Connection will query the client for some information (this part is not dependent on used ConnectionStarter) and then allow communication with the other side.

Initialisation of the connection

  1. ConnectionStarter creates the connection
  2. ConnectionStarter performs handshake
  3. ConnectionStarter creates Connection
  4. Connection queries the client
  5. the connection is initialized and Connection can be freely used

The Connection will be always created by some descendant of ConnectionStarter.


The question is: Should I include the step 4 in Connection constructor or move it to separate function that has to be called?

Source code

abstract class ConnectionStarter {
    Connection startConnection() {
        Socket socket = inializeSocket(); // Step 1
        performHandshake(socket);         // Step 2
        return new Connection(socket);    // Step 3
    }

    abstract Socket initializeSocket();
    abstract void performHandshake()
}

class Connection {
    Socket socket;

    Connection(Socket socket) {
        this.socket = socket;

        // ━━ The client will be queried now (Step 4) ━━
        queryClient()
    }

    void queryClient() { … } // Step 4
}

// Accessibility modifiers are omitted for simplicity.
\$\endgroup\$
7
  • \$\begingroup\$ Using Java is not a good idea. Use the technology that supports M:N threading model \$\endgroup\$ Commented Aug 6, 2020 at 16:31
  • \$\begingroup\$ @overexchange Code in the question is simplified a lot. I will not create single-threaded application. The real problem is that I do not want to block thread creating the connection. \$\endgroup\$ Commented Aug 6, 2020 at 17:00
  • \$\begingroup\$ Whenever there is combination of CPU + IO bound computation, prefer technology that supports M:N threading model. Java runs on 1:1 threading modle using some C library. Prefer RUST or GO \$\endgroup\$ Commented Aug 6, 2020 at 17:27
  • 5
    \$\begingroup\$ "Code in the question is simplified a lot." Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR? \$\endgroup\$ Commented Aug 6, 2020 at 19:10
  • \$\begingroup\$ @overexchange that sounds interesting do you have some materials regarding the topic that I can read. I've read for threading models on OS level from Operating System Concepts from Abraham, Silberschatz, Galvin and Gagne, but I havent got the chance to see how different languages are using this threading models \$\endgroup\$ Commented Aug 7, 2020 at 8:39

1 Answer 1

1
\$\begingroup\$

Usage of IO in the constructor can be confusing. You can call the queryClient() from ConnectionStarter:

abstract class ConnectionStarter {
    Connection startConnection() {
        Socket socket = inializeSocket();              // Step 1
        performHandshake(socket);                      // Step 2
        Connection connection = Connection(socket);    // Step 3
        connection.queryClient();
        return connection;
    }

    abstract Socket initializeSocket();
    abstract void performHandshake()
}

class Connection {
    Socket socket;

    Connection(Socket socket) {
        this.socket = socket;
    }

    void queryClient() { … } // Step 4
}

// Accessibility modifiers are omitted for simplicity.
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.