Search code examples
pythonumlobject-oriented-analysis

Class Diagram UML modelling for Events


There's a module in python called "events" from which we can import an object called "Events". URL for events module

Objects can register a callback method to the object, and other objects can invoke the member of the events. E.g. shown below:

from events import Events

#Implementation of Events
class EventGroup(Events):
  __events__ = ('eventOne', 'eventTwo', 'eventThree')

eventGroupOne = EventGroup()


#This class subscribes to the events
class Subscriber():
  def __init__(self):
    eventGroupOne.eventOne += self.onEventOne

  def onEventOne(self):
    print("EventOne invoked")


#This class invokes the events
class Invoker():
  def someFunction(self):
    eventGroupOne.eventOne()

How can we show the relationship between the classes EventGroup, Subscriber and Invoker in Class Diagram UML?


Solution

  • If you strictly follow the definitions a class diagram can be showing only used members :

    enter image description here

    In that case eventOne, eventTwo and eventThree are only visible in an object diagram


    An other way cheating a little is to consider eventOne, eventTwo and eventThree are properties of EventGroup :

    enter image description here