设计问题:客户端是否应该同时创建会话和套接字?
我有三个类:
- Client
- Session
- Socket
会话和套接字套接字依赖于客户端来创建这两个对象。
会话依赖于套接字,没有会话就不会创建套接字。 客户端是否应该有一个公开创建会话和私有创建套接字的函数?
这不是违反了得墨忒尔法则吗?
编辑: 当前代码:
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
}
现在有消息告诉我会话不应该负责从客户端获取套接字,而客户端应该创建会话。
我说得对吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于。关于
Client
,您所告诉我们的只是它创建了Session
和Socket
,仅此而已。如果
Client
需要同时使用两者,则不存在违规。如果它只是创建Socket
以便将其提供给Session
,我会说这是一种违规,并且Session
应该获取 Socket 本身。It depends. All you are telling us about
Client
is that it creates bothSession
andSocket
, nothing more.If
Client
needs to use both, then there is no violation. If it only createsSocket
in order to provide it toSession
, I would say this is a violation andSession
should getSocket
itself.