Forwarding Messages to a Receiver

It's very common nowadays to build systems that have client components communicate with server components to get something done. Even if the systems don't have explicit clients and servers, multiple peer components work together to provide the same functionality.

The quick, easy way to implement client and server communication is to have the client component send requests directly to the server. This method is also the most efficient in terms of execution overhead because no other steps or parties to the communication are in the way to slow it down.

images

If you connect clients and servers directly, one temptation is to intermix the communication code with the client and server functionality. This mixing, however, becomes a headache when the time comes to update either the client or the server code, because you need to find each and every place where you put direct communications.

Using specialized components

The Forwarder-Receiver pattern offers a way to avert maintenance headaches and improve overall encapsulation in your code with only a very minor effect on efficiency. Figure 21-1 shows the basic structure of this pattern. As you see in the figure, each client and server component (these components are called peers because they work together) has two subcomponents: a forwarder and a receiver.

Forwarding messages

When you were a schoolchild, you ...

Get Pattern-Oriented Software Architecture For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.