UML精粹--标准对象建模语言简明教程
今天借了一本《UML精粹--标准对象建模语言简明教程》(Uml Distilled: A Brief Guide to the Standard Object Modeling Language)。徐家福先生译过来的。总体上感觉:把一本好书糟蹋了。之乎者也的序自不待言。诸如“如果万一有人不小心面陷入我的陷阱,控制器对象终止,于是我就需要把兔子关入笼内,擦擦地板,重新启动系统。”之类的翻译(我查了下原著,if someone should be so careless as to fall for my trap,the controller object terminates,so I would need to put the rabbit in its cage,mop the floor,and reboot the system.),让原著中风趣的比喻变得艰涩而索然无味。而“让强制属性阙如或者示明抽象类别的例规约都是合法的。(it's legal to leave mandatory attributes empty or to show instance specifications of abstract classes.)”我不得不查看原著才明白。
尽管如此,书中(我是说原著)。还是有不少不错的内容的。于是做了些寻章摘句的工作。并附了英文原著。键盘摸得不熟,有打错的担待一下。谢谢!
泛化(generalization):
泛化的一个典型例子是涉及业务的个人客户和公司客户。个人客户和公司客户二者既有区别又有很多的类似之处。可以把类似之处放入一个通用客户类(超类型),它以个人客户类及其公司客户类为其子类型。对软件来说,明显的解释是“继承”:公司客户类是客户类的子类。
聚合与组合(aggregation and composition):
聚合是整体-部分关系。就如同说,一辆车以引擎和车轮为其两部分。它听起来很好,但困难的是什么是聚合与关联的区别。UML包含了聚合,但几乎没有任何语义。(aggregation is the part-of relationship.it's like saying that a car has an engine and wheels as its parts. This sounds good, but the difficult thing is considering what the difference is between aggregation and association. The UML included aggregation but with hardly any semantics.)
除聚合外UML具有另外定义的组合特性。点的一个实例可以是多边形的部分也可以是圆心,但不能二者兼是。通则是,虽然一个类可以是多个其它类的成分,任一实例却必须只能是一个拥有者的成分。类图可以示明可能拥有者的多重类,但是,每个实例却只能有单独一个对象为其拥有者。(as well as aggregation,the UML has the more defined proerty of composition. an instance of Point may be part of a polygon or may be the center of a circle, but it cannot be both.The class diagram may show multiple classes of potential owners, but any instance has only a single object as its owner.)
组合是示明按值拥有的特性、值对象的特性,或者对其它特定成分具有强烈并稍有排它的拥有性等特性的好方法。聚合是完全没有意义的。(Composition is a good way of showing properties that own by value, properties to value objects, or properties that have a strong and somewhat exclusive ownership of particular other components. Aggregation is strictly meaningless.)
分类与泛化(classification and generalization)
我常听人把子类型称为“是一(is a )”关系。我敦促你要提防这种思考方式。问题是“是一”这一短语可有不同的含义。
考察如下短语:
1.Shep 是一只牧羊犬。
2.牧羊犬是一只犬。
3.犬是一种动物。
4.牧羊犬是一属(Breed).。
5.犬是一种(species)。
现在试图把这些短语加以组合。如组合短语1 和2,便得"Shep 是一只犬";组合2,3得“牧羊犬是一种动物”;组合1、2、3得“Shep 是一种动物”。到目前为止,都很好。现在试1和4,便得“Shep 是一属”。组合2、5 得“牧羊犬是一种”,这就不太好了。( Now try combining the phrases. If I combine phrases 1 and 2, I get "Shep is a Dog"; 2 and 3 taken together yield "Border Collies are Animals." And 1 plus 2 plus 3 gives me "Shep is an Animal." So far, so good. Now try 1 and 4: "Shep is a Breed." The combination of 2 and 5 is "A Border Collie is a Species." These are not so good.)
为什么我可以组合其中某些短语,而不可以组合别的短语?理由是,有些是分类(classification)(对象Shep是牧羊犬类型的一个实例),有些是泛化(generalization)(牧羊犬类型是犬类型的一个子类型)。泛化是传递的,分类则不然。我可以组合后接泛化的分类,反之则不然。(Why can I combine some of these phrases and not others? The reason is that some are classification—the object Shep is an instance of the type Border Collie—and some are generalization—the type Border Collie is a subtype of the type Dog. Generalization is transitive; classification is not. I can combine a classification followed by a generalization but not vice versa.)
我提出这一点是使你对“是一(is a)”谨慎对待。使用起子类不合适以及职责混淆。在这种情形,子类型更好的检验是如下一些短语:“犬是动物的种类”和“牧羊犬的每一实例都是犬的一个实例”。( I make this point to get you to be wary of is a. Using it can lead to inappropriate use of subclassing and confused responsibilities. Better tests for subtyping in this case would be the phrases "Dogs are kinds of Animals" and "Every instance of a Border Collie is an instance of a Dog.")
附:英文原版可从fixdown下载。http://fixdown.com/soft/14631.asp
范晨鹏
------------------
软件是一种态度
成功是一种习惯