Relationship between Client and Connection

?

-

I have a Connection and a Client class with the former containing
"sendMessage(Message)" method. The Client contains several methods that
calls the "sendMessage(Message)" with the appropriate message, eg.


public class Client {

private Connection connection = ...

public Client() {
}

public void XXX() {
connection.sendMessage(...
}

public void YYY() {
connection.sendMessage(...
}

...
}

public class Connection {

public Connection(InetSocketAddress ..) {
...
}

...

public void sendMessage(....) {
...
}
...
}


The problem I'm having is in establishing the relationship between
Client and Connection especially on how each of them should be instantiated.

One way is to pass a Conection object to the Client class:

public Client (Connection connection) {
this.connection = connection;
}

But it looks kinda akward to have a Connection to be passed.

Another way is to pass an InetSocketAddress and then do

private Connection connection = createConnection();

....

void createConnection() {
connection = new Connection(inetSocketAddress);
}

but to me it looks akward too since there is no relationship between
Client and InetSocketAddress.

I'm sure there is a better and correct way to link them.

Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top