2

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 ?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Friedrich
  • 123
  • 4

3 Answers3

4

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.

Brishna Batool
  • 445
  • 3
  • 15
  • 2
    I never knew that this is called LoD (after doing that stuff for >30 years), but only under the term "information hiding". – qwerty_so Dec 15 '17 at 15:24
1

In programming it is not good to have b.c.operation()

All data should be hidden (should be private) in the class.

But if we have b.c.operation() , in the compiler it changes to (b.c).operation() so your code is equal to this code:

t=b.c;
t.operation();
1

Is this something you are looking for? You can try it out at https://www.zenuml.com.

enter image description here

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67