Search code examples
c#decoupling

Decoupling classes C#


This might be a bit vague, but I am working on a program where several classes that update the UI. I have made a "middle-man" class that basically takes all the UI requests (among other things) and routes them to the UI itself, that way the UI class only interacts with the middle-man.

The problem is that the UI class has ~20 different functions in its interface, and all my middle-man class does is basically take calls from the lower-level classes and then call an essentially identical function in the UI, which makes me wonder if this is somehow defeating the whole purpose. I'm sure this is a problem that comes up a lot. Is there any more elegant way to do this?

Thanks,

PM


Solution

  • It's nice to not have to refer to UI stuff in the backend. I assume this is the reason you're wanting to do this.

    If that's the case, what you could do is implement some Publish/subscribe pattern (such as the Observer pattern). That way, you don't have to refer specifically to the UI. You can just "publish" from your backend, and subscribe to those events from your UI.

    Alternatively, you could inherit your UI from an interface, and specify the methods you need on that. Then, refer only to the interface in your backend.