3

Since sequence diagrams depict messages between objects in a system, the messages have to be representing method calls. My question is if I have two objects in the diagram lifeline

A --- doSomething(parameters) --> B

which object is invoking the method?

Is A calling its method doSomething which in turn uses B in its implementation? Or is A sending a message to B and B is the actual object that invokes the doSomething method?

1
  • "calling a method" is simply C++ lingo for what Smalltalkers call "sending a message". IOW, they are the same thing. Jul 19, 2015 at 12:56

3 Answers 3

4

Is A calling its method doSomething which in turn uses B in its implementation? Or is A sending a message to B and B is the actual object that invokes the doSomething method?

A is calling doSomething on B.

How that happens is an implementation detail.

It could be a direct function call, where A is calling the method on B, or it could be a message, e.g. RPC, through a network, where A is requesting B to execute the method and return the result. It could even be A calling it's own function which then is calling B, if it is not doing something else important in the process.

What's important to understand is that a sequence diagram is an abstraction that shows who is doing what when. It's not so much about the details how that happens. It is not trying to write the code for you.

So if it is important that A is calling itself, that would be in the sequence diagram. But if A is calling itself only to call B, that would likely be abstracted away into A calls B.

2
  • Are you saying that it can be “Is A calling its method doSomething which in turn uses B in its implementation?” Jul 19, 2015 at 12:36
  • Hm, after rereading the question I understand what you are getting at. Editing the answer...
    – magnattic
    Jul 19, 2015 at 12:41
2

A is sending the message doSomething(parameters) to B

or

A is calling doSomething(parameters) on B

both are equivalent. I think it was when “they” where factoring eval and/or apply in scheme (lisp) that “they” realised that message passing and method calls are the same thing.

Therefore the 2nd of your 2 options.

2

"Calling a method" and "sending a message" are simply two terms from two different communities for the same thing.

When Alan Kay came up with the term "Object Orientation", he was heavily inspired by what would later become the ARPANet and then the Internet: independent machines ("objects") with their own private memory ("instance variables") that communicate with each other by sending messages.

So, in the lingo of Smalltalk and its successors, derivatives and cousins (Self, Ruby, Python, ECMAScript/JavaScript, Objective-C, Java, …) objects send messages to other objects and those objects then "respond" to that message however they see fit. The most popular reaction is to invoke a method of the same name (i.e. if you send the message foo to bar, the result will be that the foo method of bar will be invoked), but an object may also forward the message to another object or respond with a pre-calculated value (instance variable), or just blatantly ignore it.

In the lingo of Simula and its family, this is called "calling a method" and the process of deciding what to do in response is called "virtual dispatch. In the C++ family, it is called "calling a virtual function" and "vtable lookup".

But it's all more or less the same.

Personally, I prefer the messaging metaphor, because it highlights encapsulation (when you send a message to someone, you have no idea what he does with the message, the only thing you can observe is the reply), and because it evokes the analogies of handing the message off to someone else etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.