'How can I express interaction via global variable in sequence diagram
I want to draw a sequence diagram for following example.
I know that I can use message line when there is a function call interaction for data exchange. But in this case, read function interface is not defined since the target variable is defined as global for share to other components who want to read. I think all data flow between components has to be depicted during the design without considering whether it is via function interface or not. And i believe that it will give clear information about shared variable to other low level component designers.
Is there any way to draw directly shared variable in sequence diagram?
Following is my example explanation in code and what i want to depict is the variable_a
which is used between A and B.
A.h
extern unsigned char variable_a;
A.c
unsigned char variable_a;
void func_A(void)
{
variable_a = input();
}
B.c
#include "A.h"
void func_B(void)
{
if(variable_a >= 100)
{
//do something
}
else
{
//do something
}
}
Solution 1:[1]
The global variable is an object an can be shown as a separate lifeline. Access to the object can be disclosed for example with get
and set
messages.
Remark: This technique can be seen as tedious or overkill, but it has the advantage of being accurate and visualising the coupling that would otherwise remain hidden. Btw, it also encourages good practice: the less global variables involved, the less additional lifelines ;-)
Additional hint: You may be interested also in this other question about how objects involved in an interaction are known.
Solution 2:[2]
Your code could be translated to this diagram:
The global variable_a is the assignment target of the reply message and the variable is also referenced in a guard of an alt-fragment. I think this covers most needs.
It is possible to model a lifeline for the string (or unsigned char). However, in my world a string doesn't have getters or setters. Maybe it could have an asReal():Real or asInteger():Integer operation. I doubt that it would be helpful to model that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |