如何在单个图中可视化多态调用? [英] How to visualize polymorphic invocations in a single diagram?
问题描述
首先,请参见以下Java代码:
First, see these Java codes:
Drawable.java
package examples.simple.model;
public interface Drawable {
public void draw();
}
Shape.java
package examples.simple.model;
public abstract class Shape implements Drawable {
private Point center;
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
}
Rectangle.java
package examples.simple.model;
public class Rectangle extends Shape {
public void draw() {
System.out.println("Drawing a rectangle....");
}
}
Circle.java
package examples.simple.model;
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle....");
}
}
Line.java
package examples.simple.model;
public class Line implements Drawable{
public void draw() {
System.out.println("Drawing a line");
}
}
Plotter.java
package examples.simple.client;
import java.util.ArrayList;
import java.util.List;
import examples.simple.model.Circle;
import examples.simple.model.Drawable;
import examples.simple.model.Rectangle;
import examples.simple.model.Shape;
import examples.simple.model.Line;
class Plotter {
public static void main(String[] args) {
List<Drawable> drawables = new ArrayList<Drawable>();
Shape s = new Circle();
drawables.add(s);
s = new Rectangle();
drawables.add(s);
Line l = new Line();
drawables.add(l);
for (Drawable drawable : drawables) {
drawable.draw();
}
}
}
代码是多态性的经典示例.这些代码的类图是
The codes are a classical example of polymorphism. The class diagram for these code is
当我尝试使用UML序列图为这些类建模以显示多态性时,仅使用一个序列图,我需要使用四个注释来表示多态性.
When I have tried to model these classes using a UML sequence diagram to show the polymorphism, using only one sequence diagram, I have needed to use four comments to represent the polymorphism.
那么,如何在单个图表中可视化多态调用,而无需添加注释?是否存在另一种表示法或可视化(没有UML序列或通讯图)来显示多态性?在此示例中,如何在Plotter.main()
中显示调用drawable.draw()
?
So, how to visualize polymorphic invocations in a single diagram, without comments? Are there another notation or visualization (no UML sequence or communication diagram) to show polymorphism? In this example, how to show the invocation drawable.draw()
in Plotter.main()
?
推荐答案
Using a idea proposed in Factory Method Design Pattern – Sequence Diagrams, the polymorphic invocations are modeled by multiples scenarios controlled by the guard conditions. Therefore, for each polymorphic scenario, the dynamic binding (polymorphic invocation) is represented for a "scenario box". So, this is a single model (sequence diagram) to show polymorphic invocations.
从字面上看,作者的帖子评论了您的建模策略:
Literally, the author's post comments your modeling strategy:
事实证明,要对我实际的操作流程进行建模 建模多态性,后期绑定以及所有这些东西如何工作.一世 猜测在这种情况下,时序图不仅无关紧要, 但实际上令人困惑.但是我不得不尝试."
"It turns out that to model the flow of operations I am actually modeling how polymorphism, late binding and all that stuff works. I guess that in this case, a sequence diagram is not only irrelevant, but actually confusing. But I had to try."
总而言之,显示多态调用不是一件容易的事,我认为清楚地可视化多态实际上对软件工程师来说是一个开放的挑战.
To conclude, to show polymorphic invocations is a non-trivial task, and I think that clearly visualize polymorphism actually is a open challenge for software engineers.
这篇关于如何在单个图中可视化多态调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!