fordeka
fordeka

Reputation: 979

How would you regulate the invocation of methods on one class from another?

So say I have:

public interface SystemA
{
    void MethodA(string a, string b, int c);
}

and somewhere in my application,

public class ClientA
{
    public SystemA mSystemA;
    public ClientA()
    {
         mSystemA.MethodA("whatever", "ok", 42);
    }
}

but I'm trying to figure out how to change that to meet the following requirements,

  1. Calls to a method on a system may or may not receive a specific response
  2. Methods that expect responses may trigger different actions based on the time passed since sending such as calling the method again, notifying the client, or notifying the system
  3. Clients have to be able to receive messages too
  4. I want the interface of the system (or receiving client) to be the only thing that ever has to change.
  5. Minimal overhead
  6. Avoid singletons, as they crush reusability and create tight coupling

Specifically, I have three systems (among many) that are responsible for interaction with remote resources and there is no guarantee if calls to their interface will get me a response at all, or when I will get a response, or if the response will be valid- handling these cases in the systems themselves and the clients that use them is pretty complex even with states to assign the behaviors specifically.

Things I've tried or considered and don't like:

Thoughts?

Upvotes: 0

Views: 83

Answers (1)

Greg D
Greg D

Reputation: 44066

I'm not sure I understand your requirements (#1 in particular is confusing to me), but my best guess at that you're describing a typical asynchronous client-server communication mechanism.

If that's the case, consider a message queue. Calls and responses are placed in the appropriate client or server queue. Association of a call to a response is done via some sort of cookie or uid. The client can track the time that it sent the request and the time that it receives responses, etcetcetc.

Does this involve some overhead? Sure does. But if your requirements require overhead, is it really overhead?

Upvotes: 1

Related Questions