Do events really make the code untied?

So, I'm trying to use events to decouple the code I have, and here is my problem:

class WorldHandler
{
    public void Notify(object sender, EventArgs e)
    {
        if (e is CameraMovedEventArgs)
        {
            // handle event
        }

        if (e is MapLoaded)
        {
            // handle event
        }
    }
}

The WorldHandler class listens for different subsystems of my application. Does this not mean that WorldHandler is still connected to other subsystems? Wouldn't it be the same to access those subsystems inside this class directly?

If it is difficult for me to understand what I ask, I will add additional information to my post.

I did research on this issue, and I still find it confusing because different people have very different opinions on how to separate your code from events.

+3
source share
4 answers

, , ( ), , .

, . " " , , Watchee. , - EventAggregator Prism.

, ( ) - . - , .

+5

- . , . , - : " " " ", , . , , , , , .

+4

, , . , . , ( ), .

, , .NET, , , Java , .NET-.

BTW: , , . :

public void NotifyCameraMoved(object sender, EventArgs e)
{
    // handle event
}


public void NotifyMapLoaded(object sender, EventArgs e)
{
    // handle event
}
+2

Decoupling does not remove all the dependencies between all components of the program, you should not mix decoupling / communication with Cohesion , software is good when it has a free connection (communication is always present).

enter image description here

However, events help unleash the code, the sources that trigger the event, and the subscriber to it are completely different and separate objects

+2
source

All Articles