Valeriy K.
Valeriy K.

Reputation: 2904

How to represent in UML that one class calls another class method when publishing spring event

I want to draw UML diagram which represents my Spring boot application. I'm new in UML and don't know how to represent an interaction between two classes that uses other classes for it. I have a class that publishes spring events and a class which handles such events. Publisher example:

 import org.springframework.context.ApplicationEventPublisher;
    
    class Publisher {
    ApplicationEventPublisher springPublisher;
    
    public publishEvent() {
    springPublisher.publishEvent(new SomeEvent());
    }

}

Handler:

import org.springframework.context.event.EventListener;
  
 class EventHandler {
    @EventListener
    public handleEvent(Event event) {
    // some processing
    }
}

So, when I publish some events via Spring ApplicationEventPublisher my handler handles events via method annotated with @EventListener. But it is not a direct calling of the method. How I can illustrate it on the UML diagram?

enter image description here

Upvotes: 4

Views: 2626

Answers (3)

kingsjester
kingsjester

Reputation: 308

It helps a lot to understand an architecture if methods are linked in a class diagram. You can do that with a dashed line and write <<use>> on it. Of course, if the architecture is complex, you won't be able to represent all these associations.

Unfortunately a lot of people discourage to do that and purpose sequence diagram instead (which doesn't give any context of what the class contains...). Furthermore, a lot of software to represent UML class diagrams doesn't work very well to link methods together.

Upvotes: 0

Christophe
Christophe

Reputation: 73627

The class diagram

As qwerty_so rightly pointed out, in a class diagram, we do not represent interactions: we represent structure, such as classes, association between classes, and dependencies:

You may therefore want to show a dependency to the common ApplicationEvent:

enter image description here

The Spring magic

I'm not a Spring expert, but I understand that behind the "spring magic" is the annotation which hides an automatic registration of the listener to publisher, based on the signature of the annotated method.

You could be tempted to show in your model the (automatic) association between the listener and the publisher. But IMHO, this would not reflect well your design, since your model would hardwire the magic, whereas your code is dynamic in this regard. This is why the simpler diagram I show above, seems more appropriate: the magic is caused by the common Event denominator. Just use the right intermetiary type.

The interactions

If you want to show the dynamics of the interactions, it's a little more complex. You could for example use an sequence diagram, and encabulate the magic in a :SpringFramework lifeline, and materialize the automatic registration with messages sent to this "virtual" lifeline. Moreover:

  • Event sending would then be a message from the publisher to the framework,
  • Event listening would be a message sent by the framework to one or several listener.

It's not the real details. But the readers of your diagram do not need to know these details (except if they'd belong to the spring development team). So keep it as simple as possible, but not more :-)

Upvotes: 3

qwerty_so
qwerty_so

Reputation: 36333

You use either an activity or in this case probably better a sequence diagram. Those show behavior. In the SD draw a message arrow from Publisher to Eventhandler instances (life lines) and label it with the operation handleEvent().

A class diagram only shows static constructs.

Upvotes: 1

Related Questions