Graphic Android - how to look at Android's UML?

How to see UML map? UML can bring to us? This paper is to look for the answer. There are many types of UML graph, we discuss here only the most important and most commonly used two - class diagram and sequence diagram.

1 classes of Graphs

Through the class diagram, we can easily understand the code framework, clarifying the relationship between the modules, including inheritance (Inheritance), implementation (realization), dependent (dependency), composite (Composition), poly (Aggregation), Association (Association) etc.

The following 7 kinds of relationship diagrams are given on one one.

1.1 Composition

Compostion is a Association relationship, but it is more emphasized between the two kinds of global and local relations, it implies that have the same life cycle between two classes, for example three in Figure 1

1.2 Realization

      Realization is implemented in Java implements, in which an interface class interface), there is no interface defined in standard C++, but the abstract class and interface in fact have a similar function, because C++ Realization can reflect to inherit from an abstract class. In the Android C++ code, there is a special abstract class IInterface, defines some basic methods of PC interface class, details about it can reference graphic Android - Binder and Service.

1.3 Association

     The interface will be referenced in the UML, a single arrow is the most common reference (relationship) relationship. It means, an object used in the interface or an attribute of other object. Usually, the Assocation obtained by two methods

And with the combination (Composition) compared to the object, the Associated object life cycle management to complex, because he is likely to be several objects the application, if you accidentally kill an object is still in use, will cause unpredictable consequences. Java by GC complete removal of the structural analysis method, the system to determine when the object should be analyzed. C++ does not have the similar function of GC, so the smart pointer into Android in C++ (there will be special in this paper) to achieve similar GC (the difference is very big still) function, which can reduce the burden of developers, and can improve the stability of the code.

1.4 Android IPC interface.

     The C++ and Java interfaces are supported only within the process call. In order to support for interface across process, Android made some provisions. General to the interface defined by IXXXX can support IPC (of course, the process can also call IXXXX interface). When we see a IXXX interface in the diagram, we can think that this is a process of boundary, the realization of his end and call terminal object running in different processes (at least a different thread). In the figure below, two ends of the W IWindow object and WindowState object with different colors to indicate that they belong to different process, W running in the application process, and WinState and System Server process.

1.5 Aggregation

Polymerization expressed two class affiliation, but unlike Composition, their life cycle is not the same. A classic example is the factory and car, the car is factory made, the closure of the factory, the car can continue, and vice versa. PolicyManager and PhoneWindow are similar in the diagram.

1.6 Inherritance

The most common is the inheritance relationship. The inheritance relationship complex difficult to read and memory, the UML diagram is a lot easier, you can clearly see the inheritance relationship to, and can understand the design idea of inheritance. For example, right corner, PhoneWindow inherits the Window class, the Activity reference is the base class for Window objects, but really behind the director general of the PhoneWindow object, because the object of class Window Acitivy using the PolicyManager structure of the. In this way, the implementation details of PhoneWindow are PolicyManager and Window classes hide, thus greatly reducing the application program (Activity) change rate. This is one of the famous factory design mode.

1.7 dependent

Dependence with the different reference, dependence and dependence between no direct object references, usually using constant or static methods. For example in the figure, Activity uses a static makeNewWindow method of the PolicyManager class () creates the PhoneWindow object, we say that Acitivy dependent PolicyManager the module, but it does not reference the PoclicyManager object. Dependence is usually expressed by a one-way dotted arrow.

2 timing diagram

The timing diagram, we can understand the code call flow, and potential problems that may produce calls the process inspection, such as deadlock. Sequence diagram can be viewed from two directions, the longitudinal and transverse. Longitudinal describes an object to do something on a time axis, a box by representing a function call. Transverse is described each object relation between the call, including synchronous calls, asynchronous calls, returns etc. In addition, in sequence diagrams, we can assign different colors to the execution block, on behalf of their run in different processes or threads. The following is a timing diagram of the example, it describes the process to start a System server process in Android, (here is only partial, for more information please visit the graphic Android - Zygote and System Server analysis, in pink notes listed from the picture we can learn some information.


Didn't you just say from sequence diagram can be seen the deadlock? Such a pattern? Exaggerated! Yes, have a look the following example.!

This is a very simple example, there are two threads in the figure, the green is the app thread, the function calls to the MediaPlayer object to control the player, here it does two things, Start () and Stop (). The pink part represents the Driver thread, the callback function to inform the Mediaplayer at the bottom events. That is to say the Mediaplayer is a two threads simultaneously referenced objects, is a shared resource. Most probably it did not actually happen, we use a lock to protect it, he was also used to prevent conflict. So, in the picture, the green app in the Stop () when first got a lock. This is a problem, the Stop () process may be relatively long, half-way to an event, yellow notes in the right shows the situation, two color strip overlap together. This indicates that there is a resource conflict, also means that the risk of potential deadlock. We hypothesized that Stop () of the ultimate goal is to finalize the VideoDecoder object, but this time, the call to VideoDecoder (eventHandler) in another thread is not returned, behoove we need to wait for it. Unfortunately, this time a deadlock occurs, such as red annotation is shown in the picture.

The simple picture of such a figure, we can easily analyze a deadlock situation. How to solve it, as far as possible to avoid different color map in the compartmentalization of the overlapped together. Have a look at the following solution,

This time, we cancelled the lock operation, by adding a new thread (into 3 threads, 3 colors) Thread, the synchronous call into asynchronous,, to do the background processing by Thread. Because the graph is no longer in the color blocks overlap (asynchronous calls the overlap is not), Stop () and EventHandler (asynchronous) return soon, thus eliminating the existence of deadlocks. This is why Andriod designed Looper, asynchronous message handling mechanism of MessageQueue and Handler, and use a lot of in Framework, because Android is a complicated multi process / thread application environment (think of user operation for any installed applications and all kinds of unpredictable it), synchronous invocation mechanism based on lock it is difficult to guarantee the complete avoiding deadlock. About Android asynchronous message handling mechanism, can refer to graphic Android - Looper, Handler and MessageQueue a paper.

This agrees, sequence diagram are of great help to the analysis of multi thread programming analysis. We should be in the design stage as far as possible the use of class diagrams and sequence diagrams to help us avoid some common problems, help us to get a best design.

In the graphic Andrid series Bowen, we will use a large number of UML diagram to help understand and remember the code, I believe it will more clearly than parsing code simple.


3 how to draw Android UML diagram?

Tool! Must rely on tools, there are too many UML tools out there, you just need to find a support for reverse engineering, the code into the UML data structure, then the class diagram and sequence diagram drawing out step by step.

In this paper, using the tool is bouml, a free tool on the Linux (Ubuntu12.10 began not free, it is recommended in the 12.04 installation). If you don't want to start from scratch, please visit the https://github.com/samchen2009/android_uml, there is a reverse Android 4.3, and the series of all UML graphs.

Posted by Clement at November 22, 2013 - 4:21 AM