为了整理自己写的代码,重看了《UML for Java Programmers》。觉得第二章的《Working with Diagrams》特别精彩,讲清了用UML的目的,什么时候用UML,什么时候不用。
作者从为什么要建模开始讨论。
so,为什么建模?
1. Models are built to find out if something will work.
2. We investigate designs with models when the models are much cheaper than the real thing we are building.
建模在软件领域有什么作用呢?(Why build models of software?)
为了测试,不能测试的模型没有存在的意义,虽然测试UML是一个非常主观的事情。
“We make use of UML when we have something definitive we need to test, and when using UML to test it is cheaper than using code to test it.”。这里的测试对象是软件设计。你在白板上画了一个设计方案,你同事就可以检查你的设计。
此外,使用UML的目的是为了沟通和做最后的知识积累。积累下来的UML图可以让后来者快速熟悉系统。
UML图该存下来吗?
UML既然是为了沟通用,应该是用完就扔掉,不该存下来,除非“There are diagrams that record designer intent in a way that is better than code can express it.”
如何使用UML?
作者提到,任何人类能做好的事情,都是一小步一小步慢慢弄出来的,UML图也不例外。开始可以很简陋。同时,作者还建议从行为(Collaboration Diagram)入手画UML,一边调整动态的Collaboration图,一边调整静态的Class Diagram。最重要的,在画UML图时脑中要有实现的代码,不能和代码对应的UML图是空中楼阁,没有任何价值。
“Stop what you are doing, and figure out how to translate it to code.”
UML是一种语言,为了表达你的设计,只要别人理解了你的设计,就可以停止画图了。上面提到的检查你的设计,怎么检查呢?有没有标准?有标准,标准在书里也提到了,S、O、L、I、D这五个原则。当然,这是后面几章的内容。