1

Suppose you have a client-server architecture structured with a Client class that asynchronously implements the Send() and Receive() functions. You also have a base Message class and several other classes inherited from that class, based on the type of communication. This is the scenario:

  1. When a message is received, the Receive() function creates the right istance of the class which inherits from Message class, which internally implements an Handle() function.

  2. At the end of the Handle() function you need to send a message back using the Send() function of the Client.

To accomplish the point #2, which one of these two approaches represents the best-practice?

Decentralized approach:
call the Client.Send(ReplyMessage) function directly from the Handle() function in the Message. This means that all the Message classes can access and call the Send() function in the Client.

Centralized approach:
return the ReplyMessage to the Client, which will provide to call the Send() function internally. In this way there is only one point where the messages are sent.

2
  • 1
    I don't see a "best practices" distinction here. You would choose the approach that best implements your specific requirements. Jan 14, 2020 at 14:15
  • Centralization encourages consistency; decentralization encourages flexibility. We can therefore make two contradictory arguments: centralization lowers costs because changes are consistently made in one place, and decentralization lowers costs because it enables changes to be made where they are needed. Without knowing what future changes you're going to need to make to your system, it is hard to know which to recommend! It is hard to make predictions, especially about the future! Jan 14, 2020 at 19:54

2 Answers 2

1

As written i.e. you always need to send a reply, then the centralised approach is probably slightly neater. I can see two potential benefits:

  1. the common workflow (receive, handle, send) is all in one place;
  2. the handle methods, the main locus of variability, may be easier to test independently from the architecture.

The first benefit is fairly minor given the simplicity of the workflow.

The second benefit will likely be most beneficial if you have a lot of message types with complex handling methods. If you only have a handful of message types and/or the handling is simple, there probably isn't much in the decision.

0

call the Client.Send(ReplyMessage) function directly from the Handle()

This goes against the principles of loose coupling and separation of concerns, so what you called centralized approach is IMO a cleaner design:

Client.Receive(request)
response = message.Handle() 
Client.Send(response)

message is a Message attribute of Client whose concrete class (one of Message's children) is determined dynamically depending on the type of request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.