UMLclass图中如何衡量系统的性能和可维护性?

How to measure performance and maintainability of the system in UML class diagram?

我现在正在做一个 UML 测试部分,我理解性能和可维护性的一般含义,但我不明白如何用 UML class 图来评价它们。 试题为:

Consider the model depicted with the UML class diagram in the following figure.

Now consider a change of the model in terms of adding a subtotal attribute (with the value article.getPrice()*quantity) and a get method for it to the OrderLine class, and using it within getTotal(). What effect would this change have on the non-functional properties of the system?

A) worse performance of Order.getTotal(), better maintainability of the system

B) better performance of Order.getTotal(), worse maintainability of the system

C) no effect

D) worse performance of Order.getTotal(), worse maintainability of the system

E) better performance of Order.getTotal(), better maintainability of the system

正确答案是A, 所以请解释如何得出这个答案。

谢谢。

我认为 C 是正确答案。原始情况是 Order 访问 OrderLine 以获取数量,并通过相同的对象访问 Article 以获取价格。它使用这些值执行乘法(在求和周期内)。现在在新情况下,它从 Order 调用 subtotal,后者本身必须访问 Article 以获得价格,这将是 return 乘法的结果。所以从性能的角度来看,绝对没有区别。从可维护性的角度来看,subtotal 可能是更好的方法,尽管隐藏乘法的优势似乎并不明显。

场景 2 中的可维护性更好,因为 OrderLine 的封装得到改进(即维护此代码的人会感谢您 not 包括 should改为在OrderLine中。

场景 2 中的性能会更差,因为(可能的)编译器优化将意味着 getArticle().getPrice()getQuantity() 调用将优化直接访问 相同价格quantity 内存中的变量(作为 primitives 这些将在 stack 上,假设这些 get 方法是简单的访问器),而 subTotal() 不会以这种方式优化,因为它不是一个简单的访问器(即它包含逻辑本身),这意味着你因此强制遍历 heap(OrderLine 实例所在的位置)以便访问 price 变量。 栈访问比堆快得多。

因此在场景 1 中,价格和数量变量由 Order 中的循环直接在堆栈上访问。在场景 2 中,您由于调用 subTotal() 方法而强制 reference/pointer 遍历堆,这需要时间来处理,尽管现实中与今天的 CPU 和内存总线速度有微小的差异。

Stack = 基元(即价格、数量)和 references/pointers。 堆 = 对象(即 Order、OrderLine 和 Article 实例)。