Search code examples
polymorphismumlmodeling

How to visualize polymorphic invocations in a single diagram?


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

enter image description here

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.

enter image description here

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


Solution

  • 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.

    Sequence Diagram to represent polymorphic scenarios

    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.