UML for Android Engineers
Learn how to draw UML diagrams to document your Android applications. By Massimo Carli.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
UML for Android Engineers
50 mins
- Getting Started
- Understanding UML
- Deciding What to Document
- Creating Use Case Diagrams
- Creating Complex Use Case Diagrams
- Creating a Deployment Diagram
- Creating a Dependency Diagram
- Creating Class Diagrams
- Representing Relations
- Understanding Aggregation
- Representing Abstract Classes
- Representing Static Members in UML
- Representing Just What You Need: Another Example
- Using Stereotypes
- Creating Dynamic UML Diagrams
- Representing Objects
- Understanding Collaboration Diagrams
- Understanding Sequence Diagrams
- Understanding Asynchronous Invocations
- Understanding State Diagrams
- Diving Deeper Into State Diagrams
- Where to Go From Here?
Understanding Asynchronous Invocations
If your app is using some sort of asynchronous APIs like LiveData or, more generally, coroutines, how can you represent your code in a sequence diagram? Open the MovieListFragment.kt file in the ui.screen package and look at the following method:
private fun search(query: String) {
searchJob?.cancel()
searchJob = lifecycleScope.launch {
viewModel.searchMovie(query).collectLatest {
movieAdapter.submitData(it)
}
}
}
How would you represent it in a sequence diagram? Consider the following diagram that represents a simplified version of the method without the cancellation:
This is a very interesting diagram because there are some symbols for representing asynchronous messages, but there isn’t a unique way to represent them for Android. However, this sequence diagram has many interesting things to note.
- You have four different instances at the higher level of the diagram: :MovieListFragment, lifecycleScope:LifecycleCoroutineScope, :MovieViewModel and :MovieAdapter. This tells you that, in the context of the description of what happens in
search()
, they already exist. - This diagram describes what happens when some client invoked
search()
. This triggers thelaunch()
message onlifecycleScope
. It’s important to note here that thelaunch()
message is synchronous and you use a closed arrow at the end of the horizontal line. - The previous note isn’t true for the
searchMovie()
message on:MovieViewModel
. This happens in the scope of the coroutine and you can represent it as an asynchronous message using a horizontal line ending in an open arrow. -
searchMovie()
creates and returns aFlow<PagingData<UiMovie>>
. In this diagram, you make the return type explicit using a horizontal dotted line, from right to left, ending in an open arrow. This is how you represent return values in a sequence diagram. - In this diagram, you also see an activation symbol close to another. When you send the
launch()
message, you’re also passing a lambda that you can represent using the second activation symbol. - The last thing to note is the cross symbol that represents the end of the life of a particular instance. In this case, you represent the end of
Flow<PagingData<UiMovie>>
.
This diagram is also an example of how important it is to focus on a single detail of the code you want to describe. This way, you can avoid the creation of overly complex and large diagrams.
Understanding State Diagrams
The state is a fundamental concept in modern Android development. You can define a state as a set of values an item has in a specific instant. A typical example in Android is about Activity
that can be in different states that define the activity lifecycle. The Lifecycle Architecture Component defines the states you can find in the following state diagram:
This is a very simple state diagram that contains some of the main symbols you can use:
- The full circle represents the initial state. This is the state of your item when it starts to exist. As you see in the diagram, once you exit that state, you never return.
- Your system remains in the same state until “something happens”, and then it moves to another state, represented by a rectangle with rounded corners and its name inside.
- As you learned, the system moves from one state to another when “something happens”. This could be anything, like the invocation of a method or some asynchronous operation. In the diagram, the transition from CREATED to STARTED happens when the ON_CREATE happens.
- In a state diagram, you represent the final state with a circle with a smaller full black circle inside. As the name says, this is the final state, and you cannot exit from it.
There are other things you can represent in a state diagram and it’s worth having a quick look.
Diving Deeper Into State Diagrams
To learn more conventions about state diagrams, look at the following one:
This diagram contains two more pieces of information. In it, you:
- Represent a state with a round-cornered box split into two parts. The upper contains the name of the state. The lower contains the action that the system does while in that state. For instance, the system displays a spinner while in the Loading state.
- Know that you can move from one state to another when “something happens”. Most of the time, it’s because of a message or method invocation with a condition that you represent with [condition]. This means that the transition happens if the event happens and the [condition] is true.
State diagrams are one of the most useful UML diagrams you can use, but, as always, be careful to focus on a single aspect of the system you’re describing.
Where to Go From Here?
Great job completing the tutorial! You learned how to create the most important UML diagrams for documenting your app. It’s fundamental to understand that UML is a language you can modify however you want, just as long as it’s easy to understand. Very complicated diagrams with a lot of symbols and details generally do more harm than good and introduce confusion rather than clarity.
You can checkout this dedicated UML website to learn more about UML.
We hope you enjoyed this tutorial! If you have any comments or questions, feel free to join the discussion below.