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?
It depends. All you are telling us about
Clientis that it creates bothSessionandSocket, nothing more.If
Clientneeds to use both, then there is no violation. If it only createsSocketin order to provide it toSession, I would say this is a violation andSessionshould getSocketitself.