views:

26

answers:

1

I have three classes:

  • Client
  • Session
  • Socket

Both Session & Socket depeand on the Client to create both objects.
A Session depeands on a Socket and no sockets are created without a session. Should the Client have a function that creates a Session pubically and a Socket privately?
Doesn't it violate the law of demeter?
EDIT: Current code:

class Client
{
private:
  // Connection details
public:
  shared_ptr<Socket> createSocket(); // returns a new socket if the connection is opened
}

class Session
{
public:
  Session(Client &); // Accepts a client and gets a socket for i/o to the server
}

Now something tells me that the session shouldn't be responsible for getting the socket from the client and that the client should create the session.
Am I right?

A: 

It depends. All you are telling us about Client is that it creates both Session and Socket, nothing more.

If Client needs to use both, then there is no violation. If it only creates Socket in order to provide it to Session, I would say this is a violation and Session should get Socket itself.

Oded
Client contains internal objects which connect to the server.After a connection is made, currently I have a function that returns a new socket. The session object accepts a client and in the ctor it calls the function that creates the new socket for this session.Should that be changed? If so, how?
the_drow
Again, if `Client` does not use the socket, the socket creation should probably be moved to the `Session`.
Oded
But all the data required to create a socket is on the client since the internals that connect to the server are also used to create a socket (this is a given from my networking library). I can't dismiss the client class since it tracks the sessions.
the_drow
@the_drow - Perhaps you need to extract the shared information into a class of its own?
Oded
@Oded: I shall show you some code examples soon.
the_drow
@Oded: Code added. Please review the question.
the_drow
@the_drow - I would say the connection details deserve a class of their own. Create them in the `Client` and only pass that through to the `Session`.
Oded
@Oded: That way I can handle a connection pool using connection objects. Thanks.
the_drow