How to visualize polymorphic invocations in a single diagram?

北战南征 提交于 2019-12-07 18:39:58

问题


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

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.

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()?


回答1:


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.




回答2:


Sequence diagrams are strong at showing interactions, they're not very well suited to depict structural properties of a model.

A class diagram could be what you need. The below image is an example of a class diagram that shows polymorphism (namely the getArea method).




回答3:


For the reasons I put in my comment (each object having its own lifeline, each message being a single call, etc), it's difficult (if not impossible and wrong) to try to do this in a single diagram.

Furthermore, the beauty of polymorphism is that at a certain level (the polymorphic call), we don't want to know the details. A call is made to draw() and it just works, regardless of the implementing class. The details are hidden in the abstraction. UML diagrams are useful when they show the essential information.

In your answer, each of the shapes just receives the message draw() (we don't see any difference in behavior from the sequence diagram). If you really need to clarify differences between implementations of Circle, etc., I suggest representing the polymorphic calls in separate diagrams so we can see which different objects are used to achieve the draw() function, e.g. :



来源:https://stackoverflow.com/questions/25099461/how-to-visualize-polymorphic-invocations-in-a-single-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!