Inter-Process Communication - The OO WaySource: comp.object
Problem: How can an object invoke methods of another object that runs in a different process?
Klaus Elk wrote:
We wish to work with NT-processes to benefit from the protection mechanisms, that ensure that one programmer does not - by mistake - overwrite someone else's code or data. This corresponds very poorly with the use of classes.
In the old days I would use a socket/pipe based communication between processes. Now I wish to use objects - but if object A resides in another process, object B cannot simply perform "A.write" or similar.
Robert C. Martin replied:
Get the Design Patterns book and read up on the Proxy pattern. Or, get a copy of my book Designing Object Oriented C++ Applications using the Booch Method [...] and look in Chapter 4 where I outline the use of classes communicating between processes.
The main idea is rather simple. You have two classes, A and B. They live in separate processes. A wants to send the X message to B. You create an abstract class BI (B interface). This class has all the methods of B, but they are all abstract (pure virtual). B inherits from BI. Then you create another class named BProxy. BProxy also inherits from BI. BProxy knows how to use pipes and sockets to communicate with something in the process that holds B.
When A sends its message, it sends it to an object that is declared as a BI. Unbeknownst to A, the object is really a BProxy. When A calls the X method, it is executed in BProxy. BProxy takes the arguments and packages them up in a packet. It then uses the pipes and sockets to send the packet to a receiver in the process that B lives in. The receiver unpacks the packet and then calls the X method on B.
Neither A nor B know that this has happened.
This is the fundamental abstraction within an ORB.
[ORB = Object Request Broker, like COM or CORBA. Of course, you can use on of the existing ORBs instead of writing your own. -YS]
Vince Huston, A short description of the Proxy pattern
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides,
Frank Buschmann, Regine Meunier, Hans Rohnert, Peter Sommerlad and