umlsequence-diagramvisual-paradigm

UML sequence diagram, call


Let's say we have three classes A, B and C, and an instance a (resp. b/c) of type A (resp. B/C). Suppose that b is an attribute of a and c is an attribute of b.

In a method of a, the following is called : b.c.operation()

How can we represent this in a sequence diagram ?


Solution

  • According to the Law of Demeter, an object should only communicate directly with its own neighbours. So in your case, a should not be calling b.c.operation() at all, as c is not a's neighbour. Rather, class B should provide an interface for this purpose, such as doCOperation(){c.operation();} and this is what a should call.

    So the sequence of operations beocomes:

    1. a calls b.doCOperation()
    2. b calls c.Operation() within doCOperation() and returns the result to a.

    Have a go at the sequence diagram now and it should be much easier.