简体   繁体   English

在两个班级之间建立正确的关系

[英]Establishing the right relationship between two classes

For my program I have a Server class and a Protocols class. 对于我的程序,我有一个Server类和一个Protocols类。

When my Server receives a message from the Client I want the Server to send the message to the Protocols. 当我的服务器从客户端收到消息时,我希望服务器将消息发送到协议。 The Protocols then figure out what needs to be done with the message and invokes the proper methods. 然后,协议找出消息需要做什么,并调用适当的方法。 Now, the methods that need to be invoked are inside the Server. 现在,需要调用的方法在服务器内部。

So essentially, the Server needs to have access to the Protocols and the Protocols needs to have access to the Server. 因此,实质上,服务器需要访问协议,而协议需要访问服务器。

What is the best way to establish such a relationship? 建立这种关系的最佳方法是什么? How would I do it? 我该怎么办? I don't want a circular reference, but is there another way? 我不需要循环引用,但是还有另一种方法吗?

What about following the Servlet model of request/response objects? 遵循请求/响应对象的Servlet模型怎么样?

Every time you receive a message, you package it up in a request object, and you create a response object, and send it to you protocol handler (acting as a kind of servlet). 每次收到消息时,都将其打包到一个请求对象中,然后创建一个响应对象,然后将其发送给协议处理程序(充当一种servlet)。

Your handler, deals with the request, and whatever it needs to pass back, it puts it in the response object, which is ultimately used by the server to send the actual response to the client. 您的处理程序处理请求,然后将其传递回响应对象,无论它需要回传什么,服务器最终都会使用该对象将实际响应发送给客户端。 If the server needs to take any decisions, it can do it based on the information already provided in the response object after the request has been attended by your protocol handler. 如果服务器需要做出任何决定,则可以在协议处理程序处理了请求之后,根据响应对象中已经提供的信息来执行该决定。

You may later add similar concepts to those of the servlet model, like filters or event handlers to deal with similar requirements. 您稍后可以在Servlet模型中添加类似的概念,例如过滤器或事件处理程序,以处理类似的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM