简体   繁体   中英

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:

  • An event system - you have to define new events and listeners for every single new method you want to expose to the system and whenever you change the method you have to change all the events to match, all the listeners, etc. there is FAR too much overhead with using it. If you specify want the arguments for every event specified it creates a ton of work, but if you make the events really generic then you end up having to go back and forth between the sender and receivers to match them.
  • Commands - you have to define new commands for every single method on the receiver and every time you want to change the method they depend on you have to change the command. Besides that the constructor of every command would have to match the signature of a method. I feel like there would be way too much overhead with this too.

Thoughts?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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