问题
I have to model a system where an object of the class Person
will invoke the static method getBook(...) : Book
on the class Book
which will return an instance of a particular book.
How do you reference the book instance obtained by the operation?
As of now, I can think of two approaches, neither of which I have ever seen/used, which is why I am looking for the correct approach.
The first approach is to invoke methods directly on the book instance obtained, e.g. if the reference returned by getBook(...) : Book
is named matchingBook
, I would use matchingBook.doSomething(...)
, much like having a local variable.
The second way, which I find more in the line of sequence diagrams is to let the book instance returned by the operation appear with its own lifeline, e.g. next to the Book class, and referencing it with an arrow labeled doSomething(...)
.
However, with the second approach, it is not that obvious that this object is in fact the one returned by the operation.
回答1:
The second approach is the correct one. Anytime you call operations on an object returned by a first operation, you can't do better than a name match between the result of the first call and the lifeline.
Anyway I don't really understand what you expect of the first way: where would you put matchingBook.doSomething(...)
? on a arrow pointing which lifeline?
回答2:
The second approach is the correct. To show that you are pointing to the returned object (matchingBook
), you can add the variable name to the title of the lifeline, like this:
来源:https://stackoverflow.com/questions/16177894/referencing-an-instance-of-a-given-class-in-sequence-diagrams