开发者

Transposing UML into code

Short question. How do you go about transpo开发者_如何学Csing UML diagrams into code? The class diagram is an obvious one but what about others like Activity, Use Case, Sequence, State, Package, etc.?


Wow, that's really cool if you have all this prior to start coding ;) This means someone made a decent job at modeling the expected software!

That said, the various diagrams of UML can map more or less closely to the code. Here is my feeling about it generally speaking:

  • Class diagram. Maps one-to-one with code in most of the case. Can still be used during analysis to show concepts which won't map one-to-one, but it's quite rare.
  • Package diagram. Maps one-to-one with code in most of the case as well. I've never seen any usage of it other than to show the packaging of the actual sources.
  • Sequence diagram. Maps also fairly close the the code in most case. Except that some steps are frequently avoided in the sequence diagram, which would become to big otherwise. It still shows who calls who either directly or indirectly.
  • State diagram. This one is more abstract. It shows the various states of an element in the program (or the program itself) and can't be transposed as-is; you will first need to decide how the state is stored/modeled. E.g. a state diagram to show the possible thread state in java does not map to something really directly visible in the code, but threads have a state, as indicated by the corresponding enum.
  • Activity diagram. Show the individual steps of an algorithm/program. While some things can be transposed fairly easily, such as loops, the implementation of some others, such as parallel activities, can differ in the code. The most obvious implementation for parallel activities would be to use threads, but that could well be JMS, depending on the granularity of the diagram. It still wont't tell you what the logic of each step is, so there is definitively some work to interpret the diagram and implement it.
  • Use case. That's one of the most important. It can not be transposed as-is, and you will need to think how to implement the use case, which can span from something very high level to something low level. There is not formulae for it.

Conclusion: UML is a tool to describe software under various perspective. Some aspects can be transposed easily in code, some other are more abstract and can be implemented in various way, depending on the granularity of the diagrams.

This is the distinction between design and development: the implementation is the refinement of the design into something really executable (the code), which may require more or less work depending on the granularity of the existing design.

The dream would of course be to be able to generate UML out of code, and vice versa, but we are not there yet! It almost works for class diagram though :)


I have used EclipseUML Omondo for UML modeling and AndroMDA for code generation. You can generate code from Class, usecase and State diagrams using stereotypes. You need to add a stereotype on a classifier and then AndroMDA engine read the stereotype and generate code. Have a look at AndroMDA forum for more information: http://forum.andromda.org/index.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜