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 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.
精彩评论